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