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