xref: /aosp_15_r20/art/runtime/jni/jni_env_ext.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "jni_env_ext.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <algorithm>
20*795d594fSAndroid Build Coastguard Worker #include <vector>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
25*795d594fSAndroid Build Coastguard Worker #include "base/to_str.h"
26*795d594fSAndroid Build Coastguard Worker #include "check_jni.h"
27*795d594fSAndroid Build Coastguard Worker #include "hidden_api.h"
28*795d594fSAndroid Build Coastguard Worker #include "indirect_reference_table.h"
29*795d594fSAndroid Build Coastguard Worker #include "java_vm_ext.h"
30*795d594fSAndroid Build Coastguard Worker #include "jni_internal.h"
31*795d594fSAndroid Build Coastguard Worker #include "lock_word.h"
32*795d594fSAndroid Build Coastguard Worker #include "mirror/object-inl.h"
33*795d594fSAndroid Build Coastguard Worker #include "nth_caller_visitor.h"
34*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change.h"
35*795d594fSAndroid Build Coastguard Worker #include "thread-current-inl.h"
36*795d594fSAndroid Build Coastguard Worker #include "thread-inl.h"
37*795d594fSAndroid Build Coastguard Worker #include "thread_list.h"
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker static constexpr size_t kMonitorsInitial = 32;  // Arbitrary.
44*795d594fSAndroid Build Coastguard Worker static constexpr size_t kMonitorsMax = 4096;  // Maximum number of monitors held by JNI code.
45*795d594fSAndroid Build Coastguard Worker 
46*795d594fSAndroid Build Coastguard Worker const JNINativeInterface* JNIEnvExt::table_override_ = nullptr;
47*795d594fSAndroid Build Coastguard Worker 
GetEnvHandler(JavaVMExt * vm,void ** env,jint version)48*795d594fSAndroid Build Coastguard Worker jint JNIEnvExt::GetEnvHandler(JavaVMExt* vm, /*out*/void** env, jint version) {
49*795d594fSAndroid Build Coastguard Worker   UNUSED(vm);
50*795d594fSAndroid Build Coastguard Worker   // GetEnv always returns a JNIEnv* for the most current supported JNI version,
51*795d594fSAndroid Build Coastguard Worker   // and unlike other calls that take a JNI version doesn't care if you supply
52*795d594fSAndroid Build Coastguard Worker   // JNI_VERSION_1_1, which we don't otherwise support.
53*795d594fSAndroid Build Coastguard Worker   if (JavaVMExt::IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
54*795d594fSAndroid Build Coastguard Worker     return JNI_EVERSION;
55*795d594fSAndroid Build Coastguard Worker   }
56*795d594fSAndroid Build Coastguard Worker   Thread* thread = Thread::Current();
57*795d594fSAndroid Build Coastguard Worker   CHECK(thread != nullptr);
58*795d594fSAndroid Build Coastguard Worker   *env = thread->GetJniEnv();
59*795d594fSAndroid Build Coastguard Worker   return JNI_OK;
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker 
Create(Thread * self_in,JavaVMExt * vm_in,std::string * error_msg)62*795d594fSAndroid Build Coastguard Worker JNIEnvExt* JNIEnvExt::Create(Thread* self_in, JavaVMExt* vm_in, std::string* error_msg) {
63*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIEnvExt> ret(new JNIEnvExt(self_in, vm_in));
64*795d594fSAndroid Build Coastguard Worker   if (!ret->Initialize(error_msg)) {
65*795d594fSAndroid Build Coastguard Worker     return nullptr;
66*795d594fSAndroid Build Coastguard Worker   }
67*795d594fSAndroid Build Coastguard Worker   return ret.release();
68*795d594fSAndroid Build Coastguard Worker }
69*795d594fSAndroid Build Coastguard Worker 
JNIEnvExt(Thread * self_in,JavaVMExt * vm_in)70*795d594fSAndroid Build Coastguard Worker JNIEnvExt::JNIEnvExt(Thread* self_in, JavaVMExt* vm_in)
71*795d594fSAndroid Build Coastguard Worker     : self_(self_in),
72*795d594fSAndroid Build Coastguard Worker       vm_(vm_in),
73*795d594fSAndroid Build Coastguard Worker       locals_(vm_in->IsCheckJniEnabled()),
74*795d594fSAndroid Build Coastguard Worker       monitors_("monitors", kMonitorsInitial, kMonitorsMax),
75*795d594fSAndroid Build Coastguard Worker       critical_(0),
76*795d594fSAndroid Build Coastguard Worker       check_jni_(false),
77*795d594fSAndroid Build Coastguard Worker       runtime_deleted_(false) {
78*795d594fSAndroid Build Coastguard Worker   MutexLock mu(Thread::Current(), *Locks::jni_function_table_lock_);
79*795d594fSAndroid Build Coastguard Worker   check_jni_ = vm_in->IsCheckJniEnabled();
80*795d594fSAndroid Build Coastguard Worker   functions = GetFunctionTable(check_jni_);
81*795d594fSAndroid Build Coastguard Worker   unchecked_functions_ = GetJniNativeInterface();
82*795d594fSAndroid Build Coastguard Worker }
83*795d594fSAndroid Build Coastguard Worker 
Initialize(std::string * error_msg)84*795d594fSAndroid Build Coastguard Worker bool JNIEnvExt::Initialize(std::string* error_msg) {
85*795d594fSAndroid Build Coastguard Worker   return locals_.Initialize(/*max_count=*/ 1u, error_msg);
86*795d594fSAndroid Build Coastguard Worker }
87*795d594fSAndroid Build Coastguard Worker 
SetFunctionsToRuntimeShutdownFunctions()88*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::SetFunctionsToRuntimeShutdownFunctions() {
89*795d594fSAndroid Build Coastguard Worker   functions = GetRuntimeShutdownNativeInterface();
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker 
~JNIEnvExt()92*795d594fSAndroid Build Coastguard Worker JNIEnvExt::~JNIEnvExt() {
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker 
NewLocalRef(mirror::Object * obj)95*795d594fSAndroid Build Coastguard Worker jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) {
96*795d594fSAndroid Build Coastguard Worker   if (obj == nullptr) {
97*795d594fSAndroid Build Coastguard Worker     return nullptr;
98*795d594fSAndroid Build Coastguard Worker   }
99*795d594fSAndroid Build Coastguard Worker   std::string error_msg;
100*795d594fSAndroid Build Coastguard Worker   jobject ref = reinterpret_cast<jobject>(locals_.Add(obj, &error_msg));
101*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(ref == nullptr)) {
102*795d594fSAndroid Build Coastguard Worker     // This is really unexpected if we allow resizing LRTs...
103*795d594fSAndroid Build Coastguard Worker     LOG(FATAL) << error_msg;
104*795d594fSAndroid Build Coastguard Worker     UNREACHABLE();
105*795d594fSAndroid Build Coastguard Worker   }
106*795d594fSAndroid Build Coastguard Worker   return ref;
107*795d594fSAndroid Build Coastguard Worker }
108*795d594fSAndroid Build Coastguard Worker 
DeleteLocalRef(jobject obj)109*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::DeleteLocalRef(jobject obj) {
110*795d594fSAndroid Build Coastguard Worker   if (obj != nullptr) {
111*795d594fSAndroid Build Coastguard Worker     locals_.Remove(reinterpret_cast<IndirectRef>(obj));
112*795d594fSAndroid Build Coastguard Worker   }
113*795d594fSAndroid Build Coastguard Worker }
114*795d594fSAndroid Build Coastguard Worker 
SetCheckJniEnabled(bool enabled)115*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
116*795d594fSAndroid Build Coastguard Worker   check_jni_ = enabled;
117*795d594fSAndroid Build Coastguard Worker   locals_.SetCheckJniEnabled(enabled);
118*795d594fSAndroid Build Coastguard Worker   MutexLock mu(Thread::Current(), *Locks::jni_function_table_lock_);
119*795d594fSAndroid Build Coastguard Worker   functions = GetFunctionTable(enabled);
120*795d594fSAndroid Build Coastguard Worker   // Check whether this is a no-op because of override.
121*795d594fSAndroid Build Coastguard Worker   if (enabled && JNIEnvExt::table_override_ != nullptr) {
122*795d594fSAndroid Build Coastguard Worker     LOG(WARNING) << "Enabling CheckJNI after a JNIEnv function table override is not functional.";
123*795d594fSAndroid Build Coastguard Worker   }
124*795d594fSAndroid Build Coastguard Worker }
125*795d594fSAndroid Build Coastguard Worker 
DumpReferenceTables(std::ostream & os)126*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
127*795d594fSAndroid Build Coastguard Worker   locals_.Dump(os);
128*795d594fSAndroid Build Coastguard Worker   monitors_.Dump(os);
129*795d594fSAndroid Build Coastguard Worker }
130*795d594fSAndroid Build Coastguard Worker 
PushFrame(int capacity)131*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::PushFrame(int capacity) {
132*795d594fSAndroid Build Coastguard Worker   DCHECK_GE(locals_.FreeCapacity(), static_cast<size_t>(capacity));
133*795d594fSAndroid Build Coastguard Worker   stacked_local_ref_cookies_.push_back(PushLocalReferenceFrame());
134*795d594fSAndroid Build Coastguard Worker }
135*795d594fSAndroid Build Coastguard Worker 
PopFrame()136*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::PopFrame() {
137*795d594fSAndroid Build Coastguard Worker   PopLocalReferenceFrame(stacked_local_ref_cookies_.back());
138*795d594fSAndroid Build Coastguard Worker   stacked_local_ref_cookies_.pop_back();
139*795d594fSAndroid Build Coastguard Worker }
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker // Note: the offset code is brittle, as we can't use OFFSETOF_MEMBER or offsetof easily. Thus, there
142*795d594fSAndroid Build Coastguard Worker //       are tests in jni_internal_test to match the results against the actual values.
143*795d594fSAndroid Build Coastguard Worker 
144*795d594fSAndroid Build Coastguard Worker // This is encoding the knowledge of the structure and layout of JNIEnv fields.
JNIEnvSize(PointerSize pointer_size)145*795d594fSAndroid Build Coastguard Worker static size_t JNIEnvSize(PointerSize pointer_size) {
146*795d594fSAndroid Build Coastguard Worker   // A single pointer.
147*795d594fSAndroid Build Coastguard Worker   return static_cast<size_t>(pointer_size);
148*795d594fSAndroid Build Coastguard Worker }
149*795d594fSAndroid Build Coastguard Worker 
LocalReferenceTableOffset(PointerSize pointer_size)150*795d594fSAndroid Build Coastguard Worker inline MemberOffset JNIEnvExt::LocalReferenceTableOffset(PointerSize pointer_size) {
151*795d594fSAndroid Build Coastguard Worker   return MemberOffset(JNIEnvSize(pointer_size) +
152*795d594fSAndroid Build Coastguard Worker                       2 * static_cast<size_t>(pointer_size));  // Thread* self + JavaVMExt* vm
153*795d594fSAndroid Build Coastguard Worker }
154*795d594fSAndroid Build Coastguard Worker 
LrtSegmentStateOffset(PointerSize pointer_size)155*795d594fSAndroid Build Coastguard Worker MemberOffset JNIEnvExt::LrtSegmentStateOffset(PointerSize pointer_size) {
156*795d594fSAndroid Build Coastguard Worker   return MemberOffset(LocalReferenceTableOffset(pointer_size).SizeValue() +
157*795d594fSAndroid Build Coastguard Worker                       jni::LocalReferenceTable::SegmentStateOffset().SizeValue());
158*795d594fSAndroid Build Coastguard Worker }
159*795d594fSAndroid Build Coastguard Worker 
LrtPreviousStateOffset(PointerSize pointer_size)160*795d594fSAndroid Build Coastguard Worker MemberOffset JNIEnvExt::LrtPreviousStateOffset(PointerSize pointer_size) {
161*795d594fSAndroid Build Coastguard Worker   return MemberOffset(LocalReferenceTableOffset(pointer_size).SizeValue() +
162*795d594fSAndroid Build Coastguard Worker                       jni::LocalReferenceTable::PreviousStateOffset().SizeValue());
163*795d594fSAndroid Build Coastguard Worker }
164*795d594fSAndroid Build Coastguard Worker 
SelfOffset(PointerSize pointer_size)165*795d594fSAndroid Build Coastguard Worker MemberOffset JNIEnvExt::SelfOffset(PointerSize pointer_size) {
166*795d594fSAndroid Build Coastguard Worker   return MemberOffset(JNIEnvSize(pointer_size));
167*795d594fSAndroid Build Coastguard Worker }
168*795d594fSAndroid Build Coastguard Worker 
169*795d594fSAndroid Build Coastguard Worker // Use some defining part of the caller's frame as the identifying mark for the JNI segment.
GetJavaCallFrame(Thread * self)170*795d594fSAndroid Build Coastguard Worker static uintptr_t GetJavaCallFrame(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) {
171*795d594fSAndroid Build Coastguard Worker   NthCallerVisitor zeroth_caller(self, 0, false);
172*795d594fSAndroid Build Coastguard Worker   zeroth_caller.WalkStack();
173*795d594fSAndroid Build Coastguard Worker   if (zeroth_caller.caller == nullptr) {
174*795d594fSAndroid Build Coastguard Worker     // No Java code, must be from pure native code.
175*795d594fSAndroid Build Coastguard Worker     return 0;
176*795d594fSAndroid Build Coastguard Worker   } else if (zeroth_caller.GetCurrentQuickFrame() == nullptr) {
177*795d594fSAndroid Build Coastguard Worker     // Shadow frame = interpreter. Use the actual shadow frame's address.
178*795d594fSAndroid Build Coastguard Worker     DCHECK(zeroth_caller.GetCurrentShadowFrame() != nullptr);
179*795d594fSAndroid Build Coastguard Worker     return reinterpret_cast<uintptr_t>(zeroth_caller.GetCurrentShadowFrame());
180*795d594fSAndroid Build Coastguard Worker   } else {
181*795d594fSAndroid Build Coastguard Worker     // Quick frame = compiled code. Use the bottom of the frame.
182*795d594fSAndroid Build Coastguard Worker     return reinterpret_cast<uintptr_t>(zeroth_caller.GetCurrentQuickFrame());
183*795d594fSAndroid Build Coastguard Worker   }
184*795d594fSAndroid Build Coastguard Worker }
185*795d594fSAndroid Build Coastguard Worker 
RecordMonitorEnter(jobject obj)186*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::RecordMonitorEnter(jobject obj) {
187*795d594fSAndroid Build Coastguard Worker   locked_objects_.push_back(std::make_pair(GetJavaCallFrame(self_), obj));
188*795d594fSAndroid Build Coastguard Worker }
189*795d594fSAndroid Build Coastguard Worker 
ComputeMonitorDescription(Thread * self,jobject obj)190*795d594fSAndroid Build Coastguard Worker static std::string ComputeMonitorDescription(Thread* self,
191*795d594fSAndroid Build Coastguard Worker                                              jobject obj) REQUIRES_SHARED(Locks::mutator_lock_) {
192*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Object> o = self->DecodeJObject(obj);
193*795d594fSAndroid Build Coastguard Worker   if ((o->GetLockWord(false).GetState() == LockWord::kThinLocked) &&
194*795d594fSAndroid Build Coastguard Worker       Locks::mutator_lock_->IsExclusiveHeld(self)) {
195*795d594fSAndroid Build Coastguard Worker     // Getting the identity hashcode here would result in lock inflation and suspension of the
196*795d594fSAndroid Build Coastguard Worker     // current thread, which isn't safe if this is the only runnable thread.
197*795d594fSAndroid Build Coastguard Worker     return StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
198*795d594fSAndroid Build Coastguard Worker                         reinterpret_cast<intptr_t>(o.Ptr()),
199*795d594fSAndroid Build Coastguard Worker                         o->PrettyTypeOf().c_str());
200*795d594fSAndroid Build Coastguard Worker   } else {
201*795d594fSAndroid Build Coastguard Worker     // IdentityHashCode can cause thread suspension, which would invalidate o if it moved. So
202*795d594fSAndroid Build Coastguard Worker     // we get the pretty type before we call IdentityHashCode.
203*795d594fSAndroid Build Coastguard Worker     const std::string pretty_type(o->PrettyTypeOf());
204*795d594fSAndroid Build Coastguard Worker     return StringPrintf("<0x%08x> (a %s)", o->IdentityHashCode(), pretty_type.c_str());
205*795d594fSAndroid Build Coastguard Worker   }
206*795d594fSAndroid Build Coastguard Worker }
207*795d594fSAndroid Build Coastguard Worker 
RemoveMonitors(Thread * self,uintptr_t frame,ReferenceTable * monitors,std::vector<std::pair<uintptr_t,jobject>> * locked_objects)208*795d594fSAndroid Build Coastguard Worker static void RemoveMonitors(Thread* self,
209*795d594fSAndroid Build Coastguard Worker                            uintptr_t frame,
210*795d594fSAndroid Build Coastguard Worker                            ReferenceTable* monitors,
211*795d594fSAndroid Build Coastguard Worker                            std::vector<std::pair<uintptr_t, jobject>>* locked_objects)
212*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
213*795d594fSAndroid Build Coastguard Worker   auto kept_end = std::remove_if(
214*795d594fSAndroid Build Coastguard Worker       locked_objects->begin(),
215*795d594fSAndroid Build Coastguard Worker       locked_objects->end(),
216*795d594fSAndroid Build Coastguard Worker       [self, frame, monitors](const std::pair<uintptr_t, jobject>& pair)
217*795d594fSAndroid Build Coastguard Worker           REQUIRES_SHARED(Locks::mutator_lock_) {
218*795d594fSAndroid Build Coastguard Worker         if (frame == pair.first) {
219*795d594fSAndroid Build Coastguard Worker           ObjPtr<mirror::Object> o = self->DecodeJObject(pair.second);
220*795d594fSAndroid Build Coastguard Worker           monitors->Remove(o);
221*795d594fSAndroid Build Coastguard Worker           return true;
222*795d594fSAndroid Build Coastguard Worker         }
223*795d594fSAndroid Build Coastguard Worker         return false;
224*795d594fSAndroid Build Coastguard Worker       });
225*795d594fSAndroid Build Coastguard Worker   locked_objects->erase(kept_end, locked_objects->end());
226*795d594fSAndroid Build Coastguard Worker }
227*795d594fSAndroid Build Coastguard Worker 
CheckMonitorRelease(jobject obj)228*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::CheckMonitorRelease(jobject obj) {
229*795d594fSAndroid Build Coastguard Worker   uintptr_t current_frame = GetJavaCallFrame(self_);
230*795d594fSAndroid Build Coastguard Worker   std::pair<uintptr_t, jobject> exact_pair = std::make_pair(current_frame, obj);
231*795d594fSAndroid Build Coastguard Worker   auto it = std::find(locked_objects_.begin(), locked_objects_.end(), exact_pair);
232*795d594fSAndroid Build Coastguard Worker   bool will_abort = false;
233*795d594fSAndroid Build Coastguard Worker   if (it != locked_objects_.end()) {
234*795d594fSAndroid Build Coastguard Worker     locked_objects_.erase(it);
235*795d594fSAndroid Build Coastguard Worker   } else {
236*795d594fSAndroid Build Coastguard Worker     // Check whether this monitor was locked in another JNI "session."
237*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::Object> mirror_obj = self_->DecodeJObject(obj);
238*795d594fSAndroid Build Coastguard Worker     for (std::pair<uintptr_t, jobject>& pair : locked_objects_) {
239*795d594fSAndroid Build Coastguard Worker       if (self_->DecodeJObject(pair.second) == mirror_obj) {
240*795d594fSAndroid Build Coastguard Worker         std::string monitor_descr = ComputeMonitorDescription(self_, pair.second);
241*795d594fSAndroid Build Coastguard Worker         vm_->JniAbortF("<JNI MonitorExit>",
242*795d594fSAndroid Build Coastguard Worker                       "Unlocking monitor that wasn't locked here: %s",
243*795d594fSAndroid Build Coastguard Worker                       monitor_descr.c_str());
244*795d594fSAndroid Build Coastguard Worker         will_abort = true;
245*795d594fSAndroid Build Coastguard Worker         break;
246*795d594fSAndroid Build Coastguard Worker       }
247*795d594fSAndroid Build Coastguard Worker     }
248*795d594fSAndroid Build Coastguard Worker   }
249*795d594fSAndroid Build Coastguard Worker 
250*795d594fSAndroid Build Coastguard Worker   // When we abort, also make sure that any locks from the current "session" are removed from
251*795d594fSAndroid Build Coastguard Worker   // the monitors table, otherwise we may visit local objects in GC during abort (which won't be
252*795d594fSAndroid Build Coastguard Worker   // valid anymore).
253*795d594fSAndroid Build Coastguard Worker   if (will_abort) {
254*795d594fSAndroid Build Coastguard Worker     RemoveMonitors(self_, current_frame, &monitors_, &locked_objects_);
255*795d594fSAndroid Build Coastguard Worker   }
256*795d594fSAndroid Build Coastguard Worker }
257*795d594fSAndroid Build Coastguard Worker 
CheckNoHeldMonitors()258*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::CheckNoHeldMonitors() {
259*795d594fSAndroid Build Coastguard Worker   // The locked_objects_ are grouped by their stack frame component, as this enforces structured
260*795d594fSAndroid Build Coastguard Worker   // locking, and the groups form a stack. So the current frame entries are at the end. Check
261*795d594fSAndroid Build Coastguard Worker   // whether the vector is empty, and when there are elements, whether the last element belongs
262*795d594fSAndroid Build Coastguard Worker   // to this call - this signals that there are unlocked monitors.
263*795d594fSAndroid Build Coastguard Worker   if (!locked_objects_.empty()) {
264*795d594fSAndroid Build Coastguard Worker     uintptr_t current_frame = GetJavaCallFrame(self_);
265*795d594fSAndroid Build Coastguard Worker     std::pair<uintptr_t, jobject>& pair = locked_objects_[locked_objects_.size() - 1];
266*795d594fSAndroid Build Coastguard Worker     if (pair.first == current_frame) {
267*795d594fSAndroid Build Coastguard Worker       std::string monitor_descr = ComputeMonitorDescription(self_, pair.second);
268*795d594fSAndroid Build Coastguard Worker       vm_->JniAbortF("<JNI End>",
269*795d594fSAndroid Build Coastguard Worker                     "Still holding a locked object on JNI end: %s",
270*795d594fSAndroid Build Coastguard Worker                     monitor_descr.c_str());
271*795d594fSAndroid Build Coastguard Worker       // When we abort, also make sure that any locks from the current "session" are removed from
272*795d594fSAndroid Build Coastguard Worker       // the monitors table, otherwise we may visit local objects in GC during abort.
273*795d594fSAndroid Build Coastguard Worker       RemoveMonitors(self_, current_frame, &monitors_, &locked_objects_);
274*795d594fSAndroid Build Coastguard Worker     } else if (kIsDebugBuild) {
275*795d594fSAndroid Build Coastguard Worker       // Make sure there are really no other entries and our checking worked as expected.
276*795d594fSAndroid Build Coastguard Worker       for (std::pair<uintptr_t, jobject>& check_pair : locked_objects_) {
277*795d594fSAndroid Build Coastguard Worker         CHECK_NE(check_pair.first, current_frame);
278*795d594fSAndroid Build Coastguard Worker       }
279*795d594fSAndroid Build Coastguard Worker     }
280*795d594fSAndroid Build Coastguard Worker   }
281*795d594fSAndroid Build Coastguard Worker   // Ensure critical locks aren't held when returning to Java.
282*795d594fSAndroid Build Coastguard Worker   if (critical_ > 0) {
283*795d594fSAndroid Build Coastguard Worker     vm_->JniAbortF("<JNI End>",
284*795d594fSAndroid Build Coastguard Worker                   "Critical lock held when returning to Java on thread %s",
285*795d594fSAndroid Build Coastguard Worker                   ToStr<Thread>(*self_).c_str());
286*795d594fSAndroid Build Coastguard Worker   }
287*795d594fSAndroid Build Coastguard Worker }
288*795d594fSAndroid Build Coastguard Worker 
ThreadResetFunctionTable(Thread * thread,void * arg)289*795d594fSAndroid Build Coastguard Worker void ThreadResetFunctionTable(Thread* thread, [[maybe_unused]] void* arg)
290*795d594fSAndroid Build Coastguard Worker     REQUIRES(Locks::jni_function_table_lock_) {
291*795d594fSAndroid Build Coastguard Worker   JNIEnvExt* env = thread->GetJniEnv();
292*795d594fSAndroid Build Coastguard Worker   bool check_jni = env->IsCheckJniEnabled();
293*795d594fSAndroid Build Coastguard Worker   env->functions = JNIEnvExt::GetFunctionTable(check_jni);
294*795d594fSAndroid Build Coastguard Worker   env->unchecked_functions_ = GetJniNativeInterface();
295*795d594fSAndroid Build Coastguard Worker }
296*795d594fSAndroid Build Coastguard Worker 
SetTableOverride(const JNINativeInterface * table_override)297*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::SetTableOverride(const JNINativeInterface* table_override) {
298*795d594fSAndroid Build Coastguard Worker   MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
299*795d594fSAndroid Build Coastguard Worker   MutexLock mu2(Thread::Current(), *Locks::jni_function_table_lock_);
300*795d594fSAndroid Build Coastguard Worker 
301*795d594fSAndroid Build Coastguard Worker   JNIEnvExt::table_override_ = table_override;
302*795d594fSAndroid Build Coastguard Worker 
303*795d594fSAndroid Build Coastguard Worker   // See if we have a runtime. Note: we cannot run other code (like JavaVMExt's CheckJNI install
304*795d594fSAndroid Build Coastguard Worker   // code), as we'd have to recursively lock the mutex.
305*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
306*795d594fSAndroid Build Coastguard Worker   if (runtime != nullptr) {
307*795d594fSAndroid Build Coastguard Worker     runtime->GetThreadList()->ForEach(ThreadResetFunctionTable, nullptr);
308*795d594fSAndroid Build Coastguard Worker     // Core Platform API checks rely on stack walking and classifying the caller. If a table
309*795d594fSAndroid Build Coastguard Worker     // override is installed do not try to guess what semantics should be.
310*795d594fSAndroid Build Coastguard Worker     runtime->SetCorePlatformApiEnforcementPolicy(hiddenapi::EnforcementPolicy::kDisabled);
311*795d594fSAndroid Build Coastguard Worker   }
312*795d594fSAndroid Build Coastguard Worker }
313*795d594fSAndroid Build Coastguard Worker 
GetFunctionTable(bool check_jni)314*795d594fSAndroid Build Coastguard Worker const JNINativeInterface* JNIEnvExt::GetFunctionTable(bool check_jni) {
315*795d594fSAndroid Build Coastguard Worker   const JNINativeInterface* override = JNIEnvExt::table_override_;
316*795d594fSAndroid Build Coastguard Worker   if (override != nullptr) {
317*795d594fSAndroid Build Coastguard Worker     return override;
318*795d594fSAndroid Build Coastguard Worker   }
319*795d594fSAndroid Build Coastguard Worker   return check_jni ? GetCheckJniNativeInterface() : GetJniNativeInterface();
320*795d594fSAndroid Build Coastguard Worker }
321*795d594fSAndroid Build Coastguard Worker 
ResetFunctionTable()322*795d594fSAndroid Build Coastguard Worker void JNIEnvExt::ResetFunctionTable() {
323*795d594fSAndroid Build Coastguard Worker   MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
324*795d594fSAndroid Build Coastguard Worker   MutexLock mu2(Thread::Current(), *Locks::jni_function_table_lock_);
325*795d594fSAndroid Build Coastguard Worker   Runtime* runtime = Runtime::Current();
326*795d594fSAndroid Build Coastguard Worker   CHECK(runtime != nullptr);
327*795d594fSAndroid Build Coastguard Worker   runtime->GetThreadList()->ForEach(ThreadResetFunctionTable, nullptr);
328*795d594fSAndroid Build Coastguard Worker }
329*795d594fSAndroid Build Coastguard Worker 
330*795d594fSAndroid Build Coastguard Worker }  // namespace art
331