blob: 79b3438f073e8d41c327c047ee17e809fa7ec125 [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#include "jni_env_ext.h"
18
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070019#include <algorithm>
20#include <vector>
21
Ian Rogers68d8b422014-07-17 11:09:10 -070022#include "check_jni.h"
23#include "indirect_reference_table.h"
24#include "java_vm_ext.h"
25#include "jni_internal.h"
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070026#include "lock_word.h"
27#include "mirror/object-inl.h"
28#include "nth_caller_visitor.h"
29#include "thread-inl.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070030
31namespace art {
32
33static constexpr size_t kMonitorsInitial = 32; // Arbitrary.
34static constexpr size_t kMonitorsMax = 4096; // Arbitrary sanity check.
35
Andreas Gampe3f5881f2015-04-08 10:26:16 -070036// Checking "locals" requires the mutator lock, but at creation time we're really only interested
37// in validity, which isn't changing. To avoid grabbing the mutator lock, factored out and tagged
38// with NO_THREAD_SAFETY_ANALYSIS.
39static bool CheckLocalsValid(JNIEnvExt* in) NO_THREAD_SAFETY_ANALYSIS {
40 if (in == nullptr) {
41 return false;
42 }
43 return in->locals.IsValid();
44}
45
Alex Light185d1342016-08-11 10:48:03 -070046jint JNIEnvExt::GetEnvHandler(JavaVMExt* vm, /*out*/void** env, jint version) {
47 UNUSED(vm);
48 // GetEnv always returns a JNIEnv* for the most current supported JNI version,
49 // and unlike other calls that take a JNI version doesn't care if you supply
50 // JNI_VERSION_1_1, which we don't otherwise support.
51 if (JavaVMExt::IsBadJniVersion(version) && version != JNI_VERSION_1_1) {
52 return JNI_EVERSION;
53 }
54 Thread* thread = Thread::Current();
55 CHECK(thread != nullptr);
56 *env = thread->GetJniEnv();
57 return JNI_OK;
58}
59
Richard Uhlerda0a69e2016-10-11 15:06:38 +010060JNIEnvExt* JNIEnvExt::Create(Thread* self_in, JavaVMExt* vm_in, std::string* error_msg) {
61 std::unique_ptr<JNIEnvExt> ret(new JNIEnvExt(self_in, vm_in, error_msg));
Andreas Gampe3f5881f2015-04-08 10:26:16 -070062 if (CheckLocalsValid(ret.get())) {
63 return ret.release();
64 }
65 return nullptr;
66}
67
Richard Uhlerda0a69e2016-10-11 15:06:38 +010068JNIEnvExt::JNIEnvExt(Thread* self_in, JavaVMExt* vm_in, std::string* error_msg)
Andreas Gampe277ccbd2014-11-03 21:36:10 -080069 : self(self_in),
70 vm(vm_in),
Andreas Gampee03662b2016-10-13 17:12:56 -070071 local_ref_cookie(kIRTFirstSegment),
Richard Uhlerda0a69e2016-10-11 15:06:38 +010072 locals(kLocalsInitial, kLocal, error_msg),
Ian Rogers68d8b422014-07-17 11:09:10 -070073 check_jni(false),
Mathieu Chartier4d87df62016-01-07 15:14:19 -080074 runtime_deleted(false),
Ian Rogers68d8b422014-07-17 11:09:10 -070075 critical(0),
76 monitors("monitors", kMonitorsInitial, kMonitorsMax) {
77 functions = unchecked_functions = GetJniNativeInterface();
78 if (vm->IsCheckJniEnabled()) {
79 SetCheckJniEnabled(true);
80 }
81}
82
Mathieu Chartier4d87df62016-01-07 15:14:19 -080083void JNIEnvExt::SetFunctionsToRuntimeShutdownFunctions() {
84 functions = GetRuntimeShutdownNativeInterface();
85 runtime_deleted = true;
86}
87
Ian Rogers68d8b422014-07-17 11:09:10 -070088JNIEnvExt::~JNIEnvExt() {
89}
90
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070091jobject JNIEnvExt::NewLocalRef(mirror::Object* obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -070092 if (obj == nullptr) {
93 return nullptr;
94 }
95 return reinterpret_cast<jobject>(locals.Add(local_ref_cookie, obj));
96}
97
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070098void JNIEnvExt::DeleteLocalRef(jobject obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -070099 if (obj != nullptr) {
100 locals.Remove(local_ref_cookie, reinterpret_cast<IndirectRef>(obj));
101 }
102}
103
104void JNIEnvExt::SetCheckJniEnabled(bool enabled) {
105 check_jni = enabled;
106 functions = enabled ? GetCheckJniNativeInterface() : GetJniNativeInterface();
107}
108
109void JNIEnvExt::DumpReferenceTables(std::ostream& os) {
110 locals.Dump(os);
111 monitors.Dump(os);
112}
113
Roland Levillain4b8f1ec2015-08-26 18:34:03 +0100114void JNIEnvExt::PushFrame(int capacity ATTRIBUTE_UNUSED) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700115 // TODO: take 'capacity' into account.
116 stacked_local_ref_cookies.push_back(local_ref_cookie);
117 local_ref_cookie = locals.GetSegmentState();
118}
119
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700120void JNIEnvExt::PopFrame() {
Ian Rogers68d8b422014-07-17 11:09:10 -0700121 locals.SetSegmentState(local_ref_cookie);
122 local_ref_cookie = stacked_local_ref_cookies.back();
123 stacked_local_ref_cookies.pop_back();
124}
125
Andreas Gampe4d98c842015-12-09 15:14:04 -0800126// Note: the offset code is brittle, as we can't use OFFSETOF_MEMBER or offsetof easily. Thus, there
127// are tests in jni_internal_test to match the results against the actual values.
128
129// This is encoding the knowledge of the structure and layout of JNIEnv fields.
130static size_t JNIEnvSize(size_t pointer_size) {
131 // A single pointer.
132 return pointer_size;
133}
134
135Offset JNIEnvExt::SegmentStateOffset(size_t pointer_size) {
136 size_t locals_offset = JNIEnvSize(pointer_size) +
137 2 * pointer_size + // Thread* self + JavaVMExt* vm.
138 4 + // local_ref_cookie.
139 (pointer_size - 4); // Padding.
140 size_t irt_segment_state_offset =
141 IndirectReferenceTable::SegmentStateOffset(pointer_size).Int32Value();
142 return Offset(locals_offset + irt_segment_state_offset);
143}
144
145Offset JNIEnvExt::LocalRefCookieOffset(size_t pointer_size) {
146 return Offset(JNIEnvSize(pointer_size) +
147 2 * pointer_size); // Thread* self + JavaVMExt* vm
148}
149
150Offset JNIEnvExt::SelfOffset(size_t pointer_size) {
151 return Offset(JNIEnvSize(pointer_size));
Ian Rogers68d8b422014-07-17 11:09:10 -0700152}
153
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700154// Use some defining part of the caller's frame as the identifying mark for the JNI segment.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700155static uintptr_t GetJavaCallFrame(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700156 NthCallerVisitor zeroth_caller(self, 0, false);
157 zeroth_caller.WalkStack();
158 if (zeroth_caller.caller == nullptr) {
159 // No Java code, must be from pure native code.
160 return 0;
161 } else if (zeroth_caller.GetCurrentQuickFrame() == nullptr) {
162 // Shadow frame = interpreter. Use the actual shadow frame's address.
163 DCHECK(zeroth_caller.GetCurrentShadowFrame() != nullptr);
164 return reinterpret_cast<uintptr_t>(zeroth_caller.GetCurrentShadowFrame());
165 } else {
166 // Quick frame = compiled code. Use the bottom of the frame.
167 return reinterpret_cast<uintptr_t>(zeroth_caller.GetCurrentQuickFrame());
168 }
169}
170
171void JNIEnvExt::RecordMonitorEnter(jobject obj) {
172 locked_objects_.push_back(std::make_pair(GetJavaCallFrame(self), obj));
173}
174
175static std::string ComputeMonitorDescription(Thread* self,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700176 jobject obj) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700177 ObjPtr<mirror::Object> o = self->DecodeJObject(obj);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700178 if ((o->GetLockWord(false).GetState() == LockWord::kThinLocked) &&
179 Locks::mutator_lock_->IsExclusiveHeld(self)) {
180 // Getting the identity hashcode here would result in lock inflation and suspension of the
181 // current thread, which isn't safe if this is the only runnable thread.
182 return StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700183 reinterpret_cast<intptr_t>(o.Ptr()),
David Sehr709b0702016-10-13 09:12:37 -0700184 o->PrettyTypeOf().c_str());
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700185 } else {
186 // IdentityHashCode can cause thread suspension, which would invalidate o if it moved. So
187 // we get the pretty type before we call IdentityHashCode.
David Sehr709b0702016-10-13 09:12:37 -0700188 const std::string pretty_type(o->PrettyTypeOf());
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700189 return StringPrintf("<0x%08x> (a %s)", o->IdentityHashCode(), pretty_type.c_str());
190 }
191}
192
193static void RemoveMonitors(Thread* self,
194 uintptr_t frame,
195 ReferenceTable* monitors,
196 std::vector<std::pair<uintptr_t, jobject>>* locked_objects)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700197 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700198 auto kept_end = std::remove_if(
199 locked_objects->begin(),
200 locked_objects->end(),
201 [self, frame, monitors](const std::pair<uintptr_t, jobject>& pair)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700202 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700203 if (frame == pair.first) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700204 ObjPtr<mirror::Object> o = self->DecodeJObject(pair.second);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700205 monitors->Remove(o);
206 return true;
207 }
208 return false;
209 });
210 locked_objects->erase(kept_end, locked_objects->end());
211}
212
213void JNIEnvExt::CheckMonitorRelease(jobject obj) {
214 uintptr_t current_frame = GetJavaCallFrame(self);
215 std::pair<uintptr_t, jobject> exact_pair = std::make_pair(current_frame, obj);
216 auto it = std::find(locked_objects_.begin(), locked_objects_.end(), exact_pair);
217 bool will_abort = false;
218 if (it != locked_objects_.end()) {
219 locked_objects_.erase(it);
220 } else {
221 // Check whether this monitor was locked in another JNI "session."
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700222 ObjPtr<mirror::Object> mirror_obj = self->DecodeJObject(obj);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700223 for (std::pair<uintptr_t, jobject>& pair : locked_objects_) {
224 if (self->DecodeJObject(pair.second) == mirror_obj) {
225 std::string monitor_descr = ComputeMonitorDescription(self, pair.second);
226 vm->JniAbortF("<JNI MonitorExit>",
227 "Unlocking monitor that wasn't locked here: %s",
228 monitor_descr.c_str());
229 will_abort = true;
230 break;
231 }
232 }
233 }
234
235 // When we abort, also make sure that any locks from the current "session" are removed from
236 // the monitors table, otherwise we may visit local objects in GC during abort (which won't be
237 // valid anymore).
238 if (will_abort) {
239 RemoveMonitors(self, current_frame, &monitors, &locked_objects_);
240 }
241}
242
243void JNIEnvExt::CheckNoHeldMonitors() {
244 uintptr_t current_frame = GetJavaCallFrame(self);
245 // The locked_objects_ are grouped by their stack frame component, as this enforces structured
246 // locking, and the groups form a stack. So the current frame entries are at the end. Check
247 // whether the vector is empty, and when there are elements, whether the last element belongs
248 // to this call - this signals that there are unlocked monitors.
249 if (!locked_objects_.empty()) {
250 std::pair<uintptr_t, jobject>& pair = locked_objects_[locked_objects_.size() - 1];
251 if (pair.first == current_frame) {
252 std::string monitor_descr = ComputeMonitorDescription(self, pair.second);
253 vm->JniAbortF("<JNI End>",
254 "Still holding a locked object on JNI end: %s",
255 monitor_descr.c_str());
256 // When we abort, also make sure that any locks from the current "session" are removed from
257 // the monitors table, otherwise we may visit local objects in GC during abort.
258 RemoveMonitors(self, current_frame, &monitors, &locked_objects_);
259 } else if (kIsDebugBuild) {
260 // Make sure there are really no other entries and our checking worked as expected.
261 for (std::pair<uintptr_t, jobject>& check_pair : locked_objects_) {
262 CHECK_NE(check_pair.first, current_frame);
263 }
264 }
265 }
266}
267
Ian Rogers68d8b422014-07-17 11:09:10 -0700268} // namespace art