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 "obj_ptr.h"
27 #include "reference_table.h"
28 
29 namespace art {
30 
31 class JavaVMExt;
32 
33 namespace mirror {
34 class Object;
35 }  // namespace mirror
36 
37 // Number of local references in the indirect reference table. The value is arbitrary but
38 // low enough that it forces sanity checks.
39 static constexpr size_t kLocalsInitial = 512;
40 
41 struct JNIEnvExt : public JNIEnv {
42   // 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);
45 
46   ~JNIEnvExt();
47 
48   void DumpReferenceTables(std::ostream& os)
49       REQUIRES_SHARED(Locks::mutator_lock_);
50 
51   void SetCheckJniEnabled(bool enabled) REQUIRES(!Locks::jni_function_table_lock_);
52 
53   void PushFrame(int capacity) REQUIRES_SHARED(Locks::mutator_lock_);
54   void PopFrame() REQUIRES_SHARED(Locks::mutator_lock_);
55 
56   template<typename T>
57   T AddLocalReference(ObjPtr<mirror::Object> obj) REQUIRES_SHARED(Locks::mutator_lock_);
58 
59   static Offset SegmentStateOffset(size_t pointer_size);
60   static Offset LocalRefCookieOffset(size_t pointer_size);
61   static Offset SelfOffset(size_t pointer_size);
62 
63   static jint GetEnvHandler(JavaVMExt* vm, /*out*/void** out, jint version);
64 
65   jobject NewLocalRef(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_);
66   void DeleteLocalRef(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
67 
68   Thread* const self;
69   JavaVMExt* const vm;
70 
71   // Cookie used when using the local indirect reference table.
72   IRTSegmentState local_ref_cookie;
73 
74   // JNI local references.
75   IndirectReferenceTable locals GUARDED_BY(Locks::mutator_lock_);
76 
77   // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
78   // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
79   // to a native method.
80   std::vector<IRTSegmentState> stacked_local_ref_cookies;
81 
82   // Frequently-accessed fields cached from JavaVM.
83   bool check_jni;
84 
85   // If we are a JNI env for a daemon thread with a deleted runtime.
86   bool runtime_deleted;
87 
88   // How many nested "critical" JNI calls are we in?
89   int critical;
90 
91   // Entered JNI monitors, for bulk exit on thread detach.
92   ReferenceTable monitors;
93 
94   // Used by -Xcheck:jni.
95   const JNINativeInterface* unchecked_functions;
96 
97   // Functions to keep track of monitor lock and unlock operations. Used to ensure proper locking
98   // rules in CheckJNI mode.
99 
100   // Record locking of a monitor.
101   void RecordMonitorEnter(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
102 
103   // Check the release, that is, that the release is performed in the same JNI "segment."
104   void CheckMonitorRelease(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_);
105 
106   // Check that no monitors are held that have been acquired in this JNI "segment."
107   void CheckNoHeldMonitors() REQUIRES_SHARED(Locks::mutator_lock_);
108 
109   // Set the functions to the runtime shutdown functions.
110   void SetFunctionsToRuntimeShutdownFunctions();
111 
112   // Set the function table override. This will install the override (or original table, if null)
113   // to all threads.
114   // Note: JNI function table overrides are sensitive to the order of operations wrt/ CheckJNI.
115   //       After overriding the JNI function table, CheckJNI toggling is ignored.
116   static void SetTableOverride(const JNINativeInterface* table_override)
117       REQUIRES(!Locks::thread_list_lock_, !Locks::jni_function_table_lock_);
118 
119   // Return either the regular, or the CheckJNI function table. Will return table_override_ instead
120   // if it is not null.
121   static const JNINativeInterface* GetFunctionTable(bool check_jni)
122       REQUIRES(Locks::jni_function_table_lock_);
123 
124  private:
125   // Override of function tables. This applies to both default as well as instrumented (CheckJNI)
126   // function tables.
127   static const JNINativeInterface* table_override_ GUARDED_BY(Locks::jni_function_table_lock_);
128 
129   // The constructor should not be called directly. It may leave the object in an erroneous state,
130   // and the result needs to be checked.
131   JNIEnvExt(Thread* self, JavaVMExt* vm, std::string* error_msg)
132       REQUIRES(!Locks::jni_function_table_lock_);
133 
134   // All locked objects, with the (Java caller) stack frame that locked them. Used in CheckJNI
135   // to ensure that only monitors locked in this native frame are being unlocked, and that at
136   // the end all are unlocked.
137   std::vector<std::pair<uintptr_t, jobject>> locked_objects_;
138 };
139 
140 // Used to save and restore the JNIEnvExt state when not going through code created by the JNI
141 // compiler.
142 class ScopedJniEnvLocalRefState {
143  public:
ScopedJniEnvLocalRefState(JNIEnvExt * env)144   explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
145     saved_local_ref_cookie_ = env->local_ref_cookie;
146     env->local_ref_cookie = env->locals.GetSegmentState();
147   }
148 
~ScopedJniEnvLocalRefState()149   ~ScopedJniEnvLocalRefState() {
150     env_->locals.SetSegmentState(env_->local_ref_cookie);
151     env_->local_ref_cookie = saved_local_ref_cookie_;
152   }
153 
154  private:
155   JNIEnvExt* const env_;
156   IRTSegmentState saved_local_ref_cookie_;
157 
158   DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
159 };
160 
161 }  // namespace art
162 
163 #endif  // ART_RUNTIME_JNI_ENV_EXT_H_
164