blob: 2f6c5dc92ac1c2e8c49bfa09fc5e47919ce9a3d2 [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 Gampe8a2a1fc2017-09-29 17:53:18 -070048 REQUIRES_SHARED(Locks::mutator_lock_)
49 REQUIRES(!Locks::alloc_tracker_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070050
Andreas Gampec8089542017-01-16 12:41:12 -080051 void SetCheckJniEnabled(bool enabled) REQUIRES(!Locks::jni_function_table_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070052
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070053 void PushFrame(int capacity) REQUIRES_SHARED(Locks::mutator_lock_);
54 void PopFrame() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070055
56 template<typename T>
Andreas Gampe8a2a1fc2017-09-29 17:53:18 -070057 T AddLocalReference(ObjPtr<mirror::Object> obj)
58 REQUIRES_SHARED(Locks::mutator_lock_)
59 REQUIRES(!Locks::alloc_tracker_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070060
Andreas Gampe4d98c842015-12-09 15:14:04 -080061 static Offset SegmentStateOffset(size_t pointer_size);
62 static Offset LocalRefCookieOffset(size_t pointer_size);
63 static Offset SelfOffset(size_t pointer_size);
Ian Rogers68d8b422014-07-17 11:09:10 -070064
Alex Light185d1342016-08-11 10:48:03 -070065 static jint GetEnvHandler(JavaVMExt* vm, /*out*/void** out, jint version);
66
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070067 jobject NewLocalRef(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
68 void DeleteLocalRef(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070069
70 Thread* const self;
71 JavaVMExt* const vm;
72
73 // Cookie used when using the local indirect reference table.
Andreas Gampee03662b2016-10-13 17:12:56 -070074 IRTSegmentState local_ref_cookie;
Ian Rogers68d8b422014-07-17 11:09:10 -070075
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 Gampee03662b2016-10-13 17:12:56 -070082 std::vector<IRTSegmentState> stacked_local_ref_cookies;
Ian Rogers68d8b422014-07-17 11:09:10 -070083
84 // Frequently-accessed fields cached from JavaVM.
85 bool check_jni;
86
Mathieu Chartier4d87df62016-01-07 15:14:19 -080087 // If we are a JNI env for a daemon thread with a deleted runtime.
88 bool runtime_deleted;
89
Ian Rogers68d8b422014-07-17 11:09:10 -070090 // 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 Gampe3f5881f2015-04-08 10:26:16 -070098
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070099 // 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 Gampebdf7f1c2016-08-30 16:38:47 -0700103 void RecordMonitorEnter(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700104
105 // Check the release, that is, that the release is performed in the same JNI "segment."
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700106 void CheckMonitorRelease(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700107
108 // Check that no monitors are held that have been acquired in this JNI "segment."
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 void CheckNoHeldMonitors() REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700110
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800111 // Set the functions to the runtime shutdown functions.
112 void SetFunctionsToRuntimeShutdownFunctions();
113
Andreas Gampec8089542017-01-16 12:41:12 -0800114 // 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 Gampe3f5881f2015-04-08 10:26:16 -0700126 private:
Andreas Gampec8089542017-01-16 12:41:12 -0800127 // 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 Uhlerda0a69e2016-10-11 15:06:38 +0100131 // The constructor should not be called directly. It may leave the object in an erroneous state,
Andreas Gampe3f5881f2015-04-08 10:26:16 -0700132 // and the result needs to be checked.
Andreas Gampec8089542017-01-16 12:41:12 -0800133 JNIEnvExt(Thread* self, JavaVMExt* vm, std::string* error_msg)
134 REQUIRES(!Locks::jni_function_table_lock_);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700135
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 Rogers68d8b422014-07-17 11:09:10 -0700140};
141
142// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
143// compiler.
144class 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 Gampee03662b2016-10-13 17:12:56 -0700158 IRTSegmentState saved_local_ref_cookie_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700159
160 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
161};
162
163} // namespace art
164
165#endif // ART_RUNTIME_JNI_ENV_EXT_H_