blob: af933ae8356ae55380f5750cc7c7469f0241f461 [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
40struct JNIEnvExt : public JNIEnv {
Richard Uhlerda0a69e2016-10-11 15:06:38 +010041 // 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 Gampe3f5881f2015-04-08 10:26:16 -070044
Ian Rogers68d8b422014-07-17 11:09:10 -070045 ~JNIEnvExt();
46
47 void DumpReferenceTables(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070048 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070049
Andreas Gampec8089542017-01-16 12:41:12 -080050 void SetCheckJniEnabled(bool enabled) REQUIRES(!Locks::jni_function_table_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070051
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070052 void PushFrame(int capacity) REQUIRES_SHARED(Locks::mutator_lock_);
53 void PopFrame() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070054
55 template<typename T>
Mathieu Chartier8778c522016-10-04 19:06:30 -070056 T AddLocalReference(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070057
Andreas Gampe4d98c842015-12-09 15:14:04 -080058 static Offset SegmentStateOffset(size_t pointer_size);
59 static Offset LocalRefCookieOffset(size_t pointer_size);
60 static Offset SelfOffset(size_t pointer_size);
Ian Rogers68d8b422014-07-17 11:09:10 -070061
Alex Light185d1342016-08-11 10:48:03 -070062 static jint GetEnvHandler(JavaVMExt* vm, /*out*/void** out, jint version);
63
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070064 jobject NewLocalRef(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
65 void DeleteLocalRef(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070066
67 Thread* const self;
68 JavaVMExt* const vm;
69
70 // Cookie used when using the local indirect reference table.
Andreas Gampee03662b2016-10-13 17:12:56 -070071 IRTSegmentState local_ref_cookie;
Ian Rogers68d8b422014-07-17 11:09:10 -070072
73 // JNI local references.
74 IndirectReferenceTable locals GUARDED_BY(Locks::mutator_lock_);
75
76 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
77 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
78 // to a native method.
Andreas Gampee03662b2016-10-13 17:12:56 -070079 std::vector<IRTSegmentState> stacked_local_ref_cookies;
Ian Rogers68d8b422014-07-17 11:09:10 -070080
81 // Frequently-accessed fields cached from JavaVM.
82 bool check_jni;
83
Mathieu Chartier4d87df62016-01-07 15:14:19 -080084 // If we are a JNI env for a daemon thread with a deleted runtime.
85 bool runtime_deleted;
86
Ian Rogers68d8b422014-07-17 11:09:10 -070087 // How many nested "critical" JNI calls are we in?
88 int critical;
89
90 // Entered JNI monitors, for bulk exit on thread detach.
91 ReferenceTable monitors;
92
93 // Used by -Xcheck:jni.
94 const JNINativeInterface* unchecked_functions;
Andreas Gampe3f5881f2015-04-08 10:26:16 -070095
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070096 // Functions to keep track of monitor lock and unlock operations. Used to ensure proper locking
97 // rules in CheckJNI mode.
98
99 // Record locking of a monitor.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700100 void RecordMonitorEnter(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700101
102 // Check the release, that is, that the release is performed in the same JNI "segment."
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700103 void CheckMonitorRelease(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700104
105 // Check that no monitors are held that have been acquired in this JNI "segment."
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 void CheckNoHeldMonitors() REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700107
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800108 // Set the functions to the runtime shutdown functions.
109 void SetFunctionsToRuntimeShutdownFunctions();
110
Andreas Gampec8089542017-01-16 12:41:12 -0800111 // Set the function table override. This will install the override (or original table, if null)
112 // to all threads.
113 // Note: JNI function table overrides are sensitive to the order of operations wrt/ CheckJNI.
114 // After overriding the JNI function table, CheckJNI toggling is ignored.
115 static void SetTableOverride(const JNINativeInterface* table_override)
116 REQUIRES(!Locks::thread_list_lock_, !Locks::jni_function_table_lock_);
117
118 // Return either the regular, or the CheckJNI function table. Will return table_override_ instead
119 // if it is not null.
120 static const JNINativeInterface* GetFunctionTable(bool check_jni)
121 REQUIRES(Locks::jni_function_table_lock_);
122
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700123 private:
Andreas Gampec8089542017-01-16 12:41:12 -0800124 // Override of function tables. This applies to both default as well as instrumented (CheckJNI)
125 // function tables.
126 static const JNINativeInterface* table_override_ GUARDED_BY(Locks::jni_function_table_lock_);
127
Richard Uhlerda0a69e2016-10-11 15:06:38 +0100128 // The constructor should not be called directly. It may leave the object in an erroneous state,
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700129 // and the result needs to be checked.
Andreas Gampec8089542017-01-16 12:41:12 -0800130 JNIEnvExt(Thread* self, JavaVMExt* vm, std::string* error_msg)
131 REQUIRES(!Locks::jni_function_table_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700132
133 // All locked objects, with the (Java caller) stack frame that locked them. Used in CheckJNI
134 // to ensure that only monitors locked in this native frame are being unlocked, and that at
135 // the end all are unlocked.
136 std::vector<std::pair<uintptr_t, jobject>> locked_objects_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700137};
138
139// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
140// compiler.
141class ScopedJniEnvLocalRefState {
142 public:
143 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
144 saved_local_ref_cookie_ = env->local_ref_cookie;
145 env->local_ref_cookie = env->locals.GetSegmentState();
146 }
147
148 ~ScopedJniEnvLocalRefState() {
149 env_->locals.SetSegmentState(env_->local_ref_cookie);
150 env_->local_ref_cookie = saved_local_ref_cookie_;
151 }
152
153 private:
154 JNIEnvExt* const env_;
Andreas Gampee03662b2016-10-13 17:12:56 -0700155 IRTSegmentState saved_local_ref_cookie_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700156
157 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
158};
159
160} // namespace art
161
162#endif // ART_RUNTIME_JNI_ENV_EXT_H_