blob: d09e21dbdfa9af8c65fa5f0c3760337d3445722b [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001/*
Elliott Hughes0f3c5532012-03-30 14:51:51 -07002 * Copyright (C) 2012 The Android Open Source Project
Ian Rogers57b86d42012-03-27 16:05:41 -07003 *
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
Andreas Gampe57943812017-12-06 21:39:13 -080017#include <android-base/logging.h>
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Andreas Gampee03662b2016-10-13 17:12:56 -070020#include "base/casts.h"
Mathieu Chartier76433272014-09-26 14:32:37 -070021#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampee03662b2016-10-13 17:12:56 -070022#include "indirect_reference_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/object-inl.h"
Nicolas Geoffrayc3db2542021-04-20 15:50:04 +000024#include "palette/palette.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070025#include "thread-inl.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080026#include "verify_object.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070027
Nicolas Geoffrayc3db2542021-04-20 15:50:04 +000028// For methods that monitor JNI invocations and report their begin/end to
29// palette hooks.
Nicolas Geoffray4f6bb442021-06-02 18:05:51 +010030#define MONITOR_JNI(kind) \
31 { \
32 bool should_report = false; \
33 PaletteShouldReportJniInvocations(&should_report); \
34 if (should_report) { \
35 kind(self->GetJniEnv()); \
36 } \
37 }
Nicolas Geoffrayc3db2542021-04-20 15:50:04 +000038
Ian Rogers57b86d42012-03-27 16:05:41 -070039namespace art {
40
Andreas Gampee03662b2016-10-13 17:12:56 -070041static_assert(sizeof(IRTSegmentState) == sizeof(uint32_t), "IRTSegmentState size unexpected");
42static_assert(std::is_trivial<IRTSegmentState>::value, "IRTSegmentState not trivial");
43
Vladimir Marko2ca09002021-10-19 16:19:34 +000044static inline void GoToRunnableFast(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
45
Vladimir Markocedec9d2021-02-08 16:16:13 +000046extern void ReadBarrierJni(mirror::CompressedReference<mirror::Class>* declaring_class,
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070047 Thread* self ATTRIBUTE_UNUSED) {
Hiroshi Yamauchi043eb9a2016-10-14 11:21:38 -070048 DCHECK(kUseReadBarrier);
49 if (kUseBakerReadBarrier) {
Vladimir Markocedec9d2021-02-08 16:16:13 +000050 DCHECK(declaring_class->AsMirrorPtr() != nullptr)
Hiroshi Yamauchi043eb9a2016-10-14 11:21:38 -070051 << "The class of a static jni call must not be null";
52 // Check the mark bit and return early if it's already marked.
Vladimir Markocedec9d2021-02-08 16:16:13 +000053 if (LIKELY(declaring_class->AsMirrorPtr()->GetMarkBit() != 0)) {
Hiroshi Yamauchi043eb9a2016-10-14 11:21:38 -070054 return;
55 }
56 }
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070057 // Call the read barrier and update the handle.
Vladimir Markocedec9d2021-02-08 16:16:13 +000058 mirror::Class* to_ref = ReadBarrier::BarrierForRoot(declaring_class);
59 declaring_class->Assign(to_ref);
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070060}
61
Vladimir Marko2ca09002021-10-19 16:19:34 +000062// Called on entry to fast JNI, push a new local reference table only.
63extern void JniMethodFastStart(Thread* self) {
64 if (kIsDebugBuild) {
65 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
66 CHECK(native_method->IsFastNative()) << native_method->PrettyMethod();
67 }
68}
69
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Vladimir Markod95a1f22021-03-23 16:32:52 +000071extern void JniMethodStart(Thread* self) {
Vladimir Marko68e8a7c2021-03-05 08:40:22 +000072 if (kIsDebugBuild) {
73 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
74 CHECK(!native_method->IsFastNative()) << native_method->PrettyMethod();
Ian Rogers1eb512d2013-10-18 15:42:20 -070075 }
Vladimir Marko68e8a7c2021-03-05 08:40:22 +000076
77 // Transition out of runnable.
78 self->TransitionFromRunnableToSuspended(kNative);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079}
Elliott Hughesb264f082012-04-06 17:10:10 -070080
Vladimir Markod95a1f22021-03-23 16:32:52 +000081extern void JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 self->DecodeJObject(to_lock)->MonitorEnter(self);
Vladimir Markod95a1f22021-03-23 16:32:52 +000083 JniMethodStart(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070084}
85
Ian Rogers1eb512d2013-10-18 15:42:20 -070086// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
87static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko34ce1b82021-03-11 12:11:31 +000088 if (kIsDebugBuild) {
89 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
90 CHECK(!native_method->IsFastNative()) << native_method->PrettyMethod();
Igor Murashkinaf1e2992016-10-12 17:44:50 -070091 }
Vladimir Marko34ce1b82021-03-11 12:11:31 +000092
93 self->TransitionFromSuspendedToRunnable();
Igor Murashkinaf1e2992016-10-12 17:44:50 -070094}
95
Vladimir Marko2ca09002021-10-19 16:19:34 +000096ALWAYS_INLINE static inline void GoToRunnableFast(Thread* self) {
97 if (kIsDebugBuild) {
98 // Should only enter here if the method is @FastNative.
99 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
100 CHECK(native_method->IsFastNative()) << native_method->PrettyMethod();
101 }
102
103 // When we are in @FastNative, we are already Runnable.
104 // Only do a suspend check on the way out of JNI.
105 if (UNLIKELY(self->TestAllFlags())) {
106 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
107 // is a flag raised.
108 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
109 self->CheckSuspend();
110 }
111}
112
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700113static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers55256cb2017-12-21 17:07:11 -0800116 if (UNLIKELY(env->IsCheckJniEnabled())) {
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700117 env->CheckNoHeldMonitors();
118 }
Ian Rogers55256cb2017-12-21 17:07:11 -0800119 env->SetLocalSegmentState(env->GetLocalRefCookie());
120 env->SetLocalRefCookie(bit_cast<IRTSegmentState>(saved_local_ref_cookie));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121}
122
Vladimir Marko5c33d352020-02-10 12:29:13 +0000123// TODO: annotalysis disabled as monitor semantics are maintained in Java code.
124static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
125 NO_THREAD_SAFETY_ANALYSIS REQUIRES(!Roles::uninterruptible_) {
126 // Save any pending exception over monitor exit call.
127 ObjPtr<mirror::Throwable> saved_exception = nullptr;
128 if (UNLIKELY(self->IsExceptionPending())) {
129 saved_exception = self->GetException();
130 self->ClearException();
131 }
132 // Decode locked object and unlock, before popping local references.
133 self->DecodeJObject(locked)->MonitorExit(self);
134 if (UNLIKELY(self->IsExceptionPending())) {
135 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
136 << saved_exception->Dump()
137 << "\nEncountered second exception during implicit MonitorExit:\n"
138 << self->GetException()->Dump();
139 }
140 // Restore pending exception.
141 if (saved_exception != nullptr) {
142 self->SetException(saved_exception);
143 }
144}
145
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700146// TODO: These should probably be templatized or macro-ized.
147// Otherwise there's just too much repetitive boilerplate.
148
Vladimir Markod95a1f22021-03-23 16:32:52 +0000149extern void JniMethodEnd(Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700150 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151}
152
Vladimir Marko2ca09002021-10-19 16:19:34 +0000153extern void JniMethodFastEnd(Thread* self) {
154 GoToRunnableFast(self);
155}
156
Vladimir Markod95a1f22021-03-23 16:32:52 +0000157extern void JniMethodEndSynchronized(jobject locked, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700158 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160}
161
Andreas Gampe48ee3562015-04-10 19:57:29 -0700162// Common result handling for EndWithReference.
Vladimir Markod95a1f22021-03-23 16:32:52 +0000163static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result, Thread* self)
Andreas Gampe48ee3562015-04-10 19:57:29 -0700164 NO_THREAD_SAFETY_ANALYSIS {
165 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700166 ObjPtr<mirror::Object> o;
167 if (!self->IsExceptionPending()) {
168 o = self->DecodeJObject(result);
169 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 // Process result.
Ian Rogers55256cb2017-12-21 17:07:11 -0800171 if (UNLIKELY(self->GetJniEnv()->IsCheckJniEnabled())) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700172 // CheckReferenceResult can resolve types.
173 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700174 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&o));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700175 CheckReferenceResult(h_obj, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700177 VerifyObject(o);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700178 return o.Ptr();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700179}
180
Vladimir Marko2ca09002021-10-19 16:19:34 +0000181extern mirror::Object* JniMethodFastEndWithReference(jobject result, Thread* self) {
182 GoToRunnableFast(self);
183 return JniMethodEndWithReferenceHandleResult(result, self);
184}
185
Vladimir Markod95a1f22021-03-23 16:32:52 +0000186extern mirror::Object* JniMethodEndWithReference(jobject result, Thread* self) {
Andreas Gampe48ee3562015-04-10 19:57:29 -0700187 GoToRunnable(self);
Vladimir Markod95a1f22021-03-23 16:32:52 +0000188 return JniMethodEndWithReferenceHandleResult(result, self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700189}
190
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700192 jobject locked,
193 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700194 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700195 UnlockJniSynchronizedMethod(locked, self);
Vladimir Markod95a1f22021-03-23 16:32:52 +0000196 return JniMethodEndWithReferenceHandleResult(result, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700197}
198
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700199extern uint64_t GenericJniMethodEnd(Thread* self,
200 uint32_t saved_local_ref_cookie,
201 jvalue result,
202 uint64_t result_f,
Vladimir Marko6e043bb2020-02-10 16:56:54 +0000203 ArtMethod* called)
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700204 // TODO: NO_THREAD_SAFETY_ANALYSIS as GoToRunnable() is NO_THREAD_SAFETY_ANALYSIS
205 NO_THREAD_SAFETY_ANALYSIS {
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100206 bool critical_native = called->IsCriticalNative();
207 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -0700208 bool normal_native = !critical_native && !fast_native;
209
Vladimir Marko34ce1b82021-03-11 12:11:31 +0000210 // @CriticalNative does not do a state transition. @FastNative usually does not do a state
211 // transition either but it performs a suspend check that may do state transitions.
Igor Murashkin06a04e02016-09-13 15:57:37 -0700212 if (LIKELY(normal_native)) {
Nicolas Geoffray4f6bb442021-06-02 18:05:51 +0100213 MONITOR_JNI(PaletteNotifyEndJniInvocation);
Igor Murashkin06a04e02016-09-13 15:57:37 -0700214 GoToRunnable(self);
Vladimir Marko34ce1b82021-03-11 12:11:31 +0000215 } else if (fast_native) {
Vladimir Marko2ca09002021-10-19 16:19:34 +0000216 GoToRunnableFast(self);
Igor Murashkin06a04e02016-09-13 15:57:37 -0700217 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700218 // We need the mutator lock (i.e., calling GoToRunnable()) before accessing the shorty or the
219 // locked object.
Vladimir Marko5c33d352020-02-10 12:29:13 +0000220 if (called->IsSynchronized()) {
221 DCHECK(normal_native) << "@FastNative/@CriticalNative and synchronize is not supported";
Vladimir Markocedec9d2021-02-08 16:16:13 +0000222 jobject lock = GetGenericJniSynchronizationObject(self, called);
Vladimir Marko5c33d352020-02-10 12:29:13 +0000223 DCHECK(lock != nullptr);
224 UnlockJniSynchronizedMethod(lock, self);
225 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700226 char return_shorty_char = called->GetShorty()[0];
227 if (return_shorty_char == 'L') {
Vladimir Markod95a1f22021-03-23 16:32:52 +0000228 uint64_t ret =
229 reinterpret_cast<uint64_t>(JniMethodEndWithReferenceHandleResult(result.l, self));
230 PopLocalReferences(saved_local_ref_cookie, self);
231 return ret;
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700232 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700233 if (LIKELY(!critical_native)) {
Vladimir Markocedec9d2021-02-08 16:16:13 +0000234 PopLocalReferences(saved_local_ref_cookie, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -0700235 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700236 switch (return_shorty_char) {
237 case 'F': {
Vladimir Marko33bff252017-11-01 14:35:42 +0000238 if (kRuntimeISA == InstructionSet::kX86) {
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700239 // Convert back the result to float.
240 double d = bit_cast<double, uint64_t>(result_f);
241 return bit_cast<uint32_t, float>(static_cast<float>(d));
242 } else {
243 return result_f;
244 }
245 }
246 case 'D':
247 return result_f;
248 case 'Z':
249 return result.z;
250 case 'B':
251 return result.b;
252 case 'C':
253 return result.c;
254 case 'S':
255 return result.s;
256 case 'I':
257 return result.i;
258 case 'J':
259 return result.j;
260 case 'V':
261 return 0;
262 default:
263 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
Elliott Hughesc1896c92018-11-29 11:33:18 -0800264 UNREACHABLE();
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700265 }
266 }
267}
268
Vladimir Markod95a1f22021-03-23 16:32:52 +0000269extern void JniMonitoredMethodStart(Thread* self) {
270 JniMethodStart(self);
Nicolas Geoffray4f6bb442021-06-02 18:05:51 +0100271 MONITOR_JNI(PaletteNotifyBeginJniInvocation);
Nicolas Geoffrayc3db2542021-04-20 15:50:04 +0000272}
273
Vladimir Markod95a1f22021-03-23 16:32:52 +0000274extern void JniMonitoredMethodStartSynchronized(jobject to_lock, Thread* self) {
275 JniMethodStartSynchronized(to_lock, self);
Nicolas Geoffray4f6bb442021-06-02 18:05:51 +0100276 MONITOR_JNI(PaletteNotifyBeginJniInvocation);
Nicolas Geoffrayc3db2542021-04-20 15:50:04 +0000277}
278
Vladimir Markod95a1f22021-03-23 16:32:52 +0000279extern void JniMonitoredMethodEnd(Thread* self) {
Nicolas Geoffray4f6bb442021-06-02 18:05:51 +0100280 MONITOR_JNI(PaletteNotifyEndJniInvocation);
Vladimir Markod95a1f22021-03-23 16:32:52 +0000281 JniMethodEnd(self);
Nicolas Geoffrayc3db2542021-04-20 15:50:04 +0000282}
283
Vladimir Markod95a1f22021-03-23 16:32:52 +0000284extern void JniMonitoredMethodEndSynchronized(jobject locked, Thread* self) {
Nicolas Geoffray4f6bb442021-06-02 18:05:51 +0100285 MONITOR_JNI(PaletteNotifyEndJniInvocation);
Vladimir Markod95a1f22021-03-23 16:32:52 +0000286 JniMethodEndSynchronized(locked, self);
Nicolas Geoffrayc3db2542021-04-20 15:50:04 +0000287}
288
Vladimir Markod95a1f22021-03-23 16:32:52 +0000289extern mirror::Object* JniMonitoredMethodEndWithReference(jobject result, Thread* self) {
Nicolas Geoffray4f6bb442021-06-02 18:05:51 +0100290 MONITOR_JNI(PaletteNotifyEndJniInvocation);
Vladimir Markod95a1f22021-03-23 16:32:52 +0000291 return JniMethodEndWithReference(result, self);
Nicolas Geoffrayc3db2542021-04-20 15:50:04 +0000292}
293
294extern mirror::Object* JniMonitoredMethodEndWithReferenceSynchronized(
295 jobject result,
Nicolas Geoffrayc3db2542021-04-20 15:50:04 +0000296 jobject locked,
297 Thread* self) {
Nicolas Geoffray4f6bb442021-06-02 18:05:51 +0100298 MONITOR_JNI(PaletteNotifyEndJniInvocation);
Vladimir Markod95a1f22021-03-23 16:32:52 +0000299 return JniMethodEndWithReferenceSynchronized(result, locked, self);
Nicolas Geoffrayc3db2542021-04-20 15:50:04 +0000300}
301
Ian Rogers57b86d42012-03-27 16:05:41 -0700302} // namespace art