blob: 291ac48e8672fb2c3acc611163b82dbaaf38ea39 [file] [log] [blame]
Ian Rogers68d8b422014-07-17 11:09:10 -07001/*
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 Gampec15a2f42017-04-21 12:09:39 -070025#include "obj_ptr.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070026#include "reference_table.h"
27
28namespace art {
29
30class JavaVMExt;
31
Andreas Gampec15a2f42017-04-21 12:09:39 -070032namespace mirror {
33class Object;
34} // namespace mirror
35
Andreas Gampea8e3b862016-10-17 20:12:52 -070036// Number of local references in the indirect reference table. The value is arbitrary but
Ian Rogers68d8b422014-07-17 11:09:10 -070037// low enough that it forces sanity checks.
Andreas Gampea8e3b862016-10-17 20:12:52 -070038static constexpr size_t kLocalsInitial = 512;
Ian Rogers68d8b422014-07-17 11:09:10 -070039
Ian Rogers55256cb2017-12-21 17:07:11 -080040class JNIEnvExt : public JNIEnv {
41 public:
Richard Uhlerda0a69e2016-10-11 15:06:38 +010042 // Creates a new JNIEnvExt. Returns null on error, in which case error_msg
43 // will contain a description of the error.
44 static JNIEnvExt* Create(Thread* self, JavaVMExt* vm, std::string* error_msg);
Ian Rogers55256cb2017-12-21 17:07:11 -080045 static Offset SegmentStateOffset(size_t pointer_size);
46 static Offset LocalRefCookieOffset(size_t pointer_size);
47 static Offset SelfOffset(size_t pointer_size);
48 static jint GetEnvHandler(JavaVMExt* vm, /*out*/void** out, jint version);
Andreas Gampe3f5881f2015-04-08 10:26:16 -070049
Ian Rogers68d8b422014-07-17 11:09:10 -070050 ~JNIEnvExt();
51
52 void DumpReferenceTables(std::ostream& os)
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -070053 REQUIRES_SHARED(Locks::mutator_lock_)
54 REQUIRES(!Locks::alloc_tracker_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070055
Andreas Gampec8089542017-01-16 12:41:12 -080056 void SetCheckJniEnabled(bool enabled) REQUIRES(!Locks::jni_function_table_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070057
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070058 void PushFrame(int capacity) REQUIRES_SHARED(Locks::mutator_lock_);
59 void PopFrame() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070060
61 template<typename T>
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -070062 T AddLocalReference(ObjPtr<mirror::Object> obj)
63 REQUIRES_SHARED(Locks::mutator_lock_)
64 REQUIRES(!Locks::alloc_tracker_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070065
Ian Rogers55256cb2017-12-21 17:07:11 -080066 void UpdateLocal(IndirectRef iref, ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_) {
67 locals_.Update(iref, obj);
68 }
Alex Light185d1342016-08-11 10:48:03 -070069
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070070 jobject NewLocalRef(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
71 void DeleteLocalRef(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070072
Ian Rogers55256cb2017-12-21 17:07:11 -080073 void TrimLocals() REQUIRES_SHARED(Locks::mutator_lock_) {
74 locals_.Trim();
75 }
76 void AssertLocalsEmpty() REQUIRES_SHARED(Locks::mutator_lock_) {
77 locals_.AssertEmpty();
78 }
79 size_t GetLocalsCapacity() REQUIRES_SHARED(Locks::mutator_lock_) {
80 return locals_.Capacity();
81 }
Ian Rogers68d8b422014-07-17 11:09:10 -070082
Ian Rogers55256cb2017-12-21 17:07:11 -080083 IRTSegmentState GetLocalRefCookie() const { return local_ref_cookie_; }
84 void SetLocalRefCookie(IRTSegmentState new_cookie) { local_ref_cookie_ = new_cookie; }
Ian Rogers68d8b422014-07-17 11:09:10 -070085
Ian Rogers55256cb2017-12-21 17:07:11 -080086 IRTSegmentState GetLocalsSegmentState() const REQUIRES_SHARED(Locks::mutator_lock_) {
87 return locals_.GetSegmentState();
88 }
89 void SetLocalSegmentState(IRTSegmentState new_state) REQUIRES_SHARED(Locks::mutator_lock_) {
90 locals_.SetSegmentState(new_state);
91 }
Ian Rogers68d8b422014-07-17 11:09:10 -070092
Ian Rogers55256cb2017-12-21 17:07:11 -080093 void VisitJniLocalRoots(RootVisitor* visitor, const RootInfo& root_info)
94 REQUIRES_SHARED(Locks::mutator_lock_) {
95 locals_.VisitRoots(visitor, root_info);
96 }
Ian Rogers68d8b422014-07-17 11:09:10 -070097
Ian Rogers55256cb2017-12-21 17:07:11 -080098 Thread* GetSelf() const { return self_; }
Ian Rogersac4d45a2018-02-15 11:19:01 -080099 uint32_t GetCritical() const { return critical_; }
100 void SetCritical(uint32_t new_critical) { critical_ = new_critical; }
101 uint64_t GetCriticalStartUs() const { return critical_start_us_; }
102 void SetCriticalStartUs(uint64_t new_critical_start_us) {
103 critical_start_us_ = new_critical_start_us;
104 }
105 const JNINativeInterface* GetUncheckedFunctions() const {
106 return unchecked_functions_;
107 }
Ian Rogers55256cb2017-12-21 17:07:11 -0800108 JavaVMExt* GetVm() const { return vm_; }
Ian Rogers68d8b422014-07-17 11:09:10 -0700109
Ian Rogers55256cb2017-12-21 17:07:11 -0800110 bool IsRuntimeDeleted() const { return runtime_deleted_; }
111 bool IsCheckJniEnabled() const { return check_jni_; }
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800112
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700113
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700114 // Functions to keep track of monitor lock and unlock operations. Used to ensure proper locking
115 // rules in CheckJNI mode.
116
117 // Record locking of a monitor.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700118 void RecordMonitorEnter(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700119
120 // Check the release, that is, that the release is performed in the same JNI "segment."
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700121 void CheckMonitorRelease(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700122
123 // Check that no monitors are held that have been acquired in this JNI "segment."
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700124 void CheckNoHeldMonitors() REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700125
Ian Rogers55256cb2017-12-21 17:07:11 -0800126 void VisitMonitorRoots(RootVisitor* visitor, const RootInfo& root_info)
127 REQUIRES_SHARED(Locks::mutator_lock_) {
128 monitors_.VisitRoots(visitor, root_info);
129 }
130
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800131 // Set the functions to the runtime shutdown functions.
132 void SetFunctionsToRuntimeShutdownFunctions();
133
Andreas Gampec8089542017-01-16 12:41:12 -0800134 // Set the function table override. This will install the override (or original table, if null)
135 // to all threads.
136 // Note: JNI function table overrides are sensitive to the order of operations wrt/ CheckJNI.
137 // After overriding the JNI function table, CheckJNI toggling is ignored.
138 static void SetTableOverride(const JNINativeInterface* table_override)
139 REQUIRES(!Locks::thread_list_lock_, !Locks::jni_function_table_lock_);
140
141 // Return either the regular, or the CheckJNI function table. Will return table_override_ instead
142 // if it is not null.
143 static const JNINativeInterface* GetFunctionTable(bool check_jni)
144 REQUIRES(Locks::jni_function_table_lock_);
145
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700146 private:
Ian Rogers55256cb2017-12-21 17:07:11 -0800147 // Checking "locals" requires the mutator lock, but at creation time we're
148 // really only interested in validity, which isn't changing. To avoid grabbing
149 // the mutator lock, factored out and tagged with NO_THREAD_SAFETY_ANALYSIS.
150 static bool CheckLocalsValid(JNIEnvExt* in) NO_THREAD_SAFETY_ANALYSIS;
151
Andreas Gampec8089542017-01-16 12:41:12 -0800152 // Override of function tables. This applies to both default as well as instrumented (CheckJNI)
153 // function tables.
154 static const JNINativeInterface* table_override_ GUARDED_BY(Locks::jni_function_table_lock_);
155
Richard Uhlerda0a69e2016-10-11 15:06:38 +0100156 // The constructor should not be called directly. It may leave the object in an erroneous state,
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700157 // and the result needs to be checked.
Andreas Gampec8089542017-01-16 12:41:12 -0800158 JNIEnvExt(Thread* self, JavaVMExt* vm, std::string* error_msg)
159 REQUIRES(!Locks::jni_function_table_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700160
Ian Rogers55256cb2017-12-21 17:07:11 -0800161 // Link to Thread::Current().
162 Thread* const self_;
163
164 // The invocation interface JavaVM.
165 JavaVMExt* const vm_;
166
167 // Cookie used when using the local indirect reference table.
168 IRTSegmentState local_ref_cookie_;
169
170 // JNI local references.
171 IndirectReferenceTable locals_ GUARDED_BY(Locks::mutator_lock_);
172
173 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
174 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
175 // to a native method.
176 std::vector<IRTSegmentState> stacked_local_ref_cookies_;
177
178 // Entered JNI monitors, for bulk exit on thread detach.
179 ReferenceTable monitors_;
180
181 // Used by -Xcheck:jni.
182 const JNINativeInterface* unchecked_functions_;
183
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700184 // All locked objects, with the (Java caller) stack frame that locked them. Used in CheckJNI
185 // to ensure that only monitors locked in this native frame are being unlocked, and that at
186 // the end all are unlocked.
187 std::vector<std::pair<uintptr_t, jobject>> locked_objects_;
Ian Rogers55256cb2017-12-21 17:07:11 -0800188
189 // Start time of "critical" JNI calls to ensure that their use doesn't
190 // excessively block the VM with CheckJNI.
191 uint64_t critical_start_us_;
192
193 // How many nested "critical" JNI calls are we in? Used by CheckJNI to ensure that criticals are
194 uint32_t critical_;
195
196 // Frequently-accessed fields cached from JavaVM.
197 bool check_jni_;
198
199 // If we are a JNI env for a daemon thread with a deleted runtime.
200 bool runtime_deleted_;
201
Ian Rogers55256cb2017-12-21 17:07:11 -0800202 friend class JNI;
Ian Rogers55256cb2017-12-21 17:07:11 -0800203 friend class ScopedJniEnvLocalRefState;
204 friend class Thread;
205 ART_FRIEND_TEST(JniInternalTest, JNIEnvExtOffsets);
Ian Rogers68d8b422014-07-17 11:09:10 -0700206};
207
208// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
209// compiler.
210class ScopedJniEnvLocalRefState {
211 public:
Ian Rogers55256cb2017-12-21 17:07:11 -0800212 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) :
213 env_(env),
214 saved_local_ref_cookie_(env->local_ref_cookie_) {
215 env->local_ref_cookie_ = env->locals_.GetSegmentState();
Ian Rogers68d8b422014-07-17 11:09:10 -0700216 }
217
218 ~ScopedJniEnvLocalRefState() {
Ian Rogers55256cb2017-12-21 17:07:11 -0800219 env_->locals_.SetSegmentState(env_->local_ref_cookie_);
220 env_->local_ref_cookie_ = saved_local_ref_cookie_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700221 }
222
223 private:
224 JNIEnvExt* const env_;
Ian Rogers55256cb2017-12-21 17:07:11 -0800225 const IRTSegmentState saved_local_ref_cookie_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700226
227 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
228};
229
230} // namespace art
231
232#endif // ART_RUNTIME_JNI_ENV_EXT_H_