blob: 23a28f9cce905d5580f706b2406a0cc263865863 [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
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070017#include "dex_file-inl.h"
Ian Rogers7655f292013-07-29 11:07:13 -070018#include "entrypoints/entrypoint_utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/class-inl.h"
20#include "mirror/abstract_method-inl.h"
21#include "mirror/object.h"
22#include "mirror/object-inl.h"
23#include "mirror/object_array-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070024#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070026#include "thread.h"
27
28namespace art {
29
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080031extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers120f1c72012-09-28 17:17:10 -070033 DCHECK(env != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
35 env->local_ref_cookie = env->locals.GetSegmentState();
36 self->TransitionFromRunnableToSuspended(kNative);
37 return saved_local_ref_cookie;
38}
Elliott Hughesb264f082012-04-06 17:10:10 -070039
Ian Rogers693ff612013-02-01 10:56:12 -080040extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 self->DecodeJObject(to_lock)->MonitorEnter(self);
42 return JniMethodStart(self);
43}
44
45static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self) {
46 JNIEnvExt* env = self->GetJniEnv();
47 env->locals.SetSegmentState(env->local_ref_cookie);
48 env->local_ref_cookie = saved_local_ref_cookie;
49 self->PopSirt();
50}
51
Ian Rogers693ff612013-02-01 10:56:12 -080052extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 self->TransitionFromSuspendedToRunnable();
54 PopLocalReferences(saved_local_ref_cookie, self);
55}
56
57
Ian Rogers693ff612013-02-01 10:56:12 -080058extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
59 Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060 self->TransitionFromSuspendedToRunnable();
61 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
62 PopLocalReferences(saved_local_ref_cookie, self);
63}
64
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080066 Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067 self->TransitionFromSuspendedToRunnable();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 mirror::Object* o = self->DecodeJObject(result); // Must decode before pop.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069 PopLocalReferences(saved_local_ref_cookie, self);
70 // Process result.
71 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
72 if (self->IsExceptionPending()) {
73 return NULL;
74 }
75 CheckReferenceResult(o, self);
76 }
77 return o;
78}
79
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
81 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080082 jobject locked, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 self->TransitionFromSuspendedToRunnable();
84 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 mirror::Object* o = self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086 PopLocalReferences(saved_local_ref_cookie, self);
87 // Process result.
88 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
89 if (self->IsExceptionPending()) {
90 return NULL;
91 }
92 CheckReferenceResult(o, self);
93 }
Elliott Hughesb264f082012-04-06 17:10:10 -070094 return o;
Ian Rogers57b86d42012-03-27 16:05:41 -070095}
96
97static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
98 intptr_t value = *arg_ptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 mirror::Object** value_as_jni_rep = reinterpret_cast<mirror::Object**>(value);
100 mirror::Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
101 CHECK(Runtime::Current()->GetHeap()->IsHeapAddress(value_as_work_around_rep))
102 << value_as_work_around_rep;
Ian Rogers57b86d42012-03-27 16:05:41 -0700103 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
104}
105
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp)
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700108 DCHECK(Thread::Current() == self);
109 // TODO: this code is specific to ARM
110 // On entry the stack pointed by sp is:
111 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
112 // | LR |
113 // | R3 | arg2
114 // | R2 | arg1
115 // | R1 | jclass/jobject
116 // | R0 | JNIEnv
117 // | unused |
118 // | unused |
119 // | unused | <- sp
Ian Rogers62d6c772013-02-27 08:32:07 -0800120 mirror::AbstractMethod* jni_method = self->GetCurrentMethod(NULL);
Ian Rogers57b86d42012-03-27 16:05:41 -0700121 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
122 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
123 // Fix up this/jclass argument
124 WorkAroundJniBugsForJobject(arg_ptr);
125 arg_ptr++;
126 // Fix up jobject arguments
127 MethodHelper mh(jni_method);
128 int reg_num = 2; // Current register being processed, -1 for stack arguments.
129 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
130 char shorty_char = mh.GetShorty()[i];
131 if (shorty_char == 'L') {
132 WorkAroundJniBugsForJobject(arg_ptr);
133 }
134 if (shorty_char == 'J' || shorty_char == 'D') {
135 if (reg_num == 2) {
136 arg_ptr = sp + 8; // skip to out arguments
137 reg_num = -1;
138 } else if (reg_num == 3) {
139 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
140 reg_num = -1;
141 } else {
Elliott Hughes74847412012-06-20 18:10:21 -0700142 DCHECK_EQ(reg_num, -1);
Ian Rogers57b86d42012-03-27 16:05:41 -0700143 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
144 arg_ptr += 3; // unaligned, pad and move through stack arguments
145 } else {
146 arg_ptr += 2; // aligned, move through stack arguments
147 }
148 }
149 } else {
150 if (reg_num == 2) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700151 arg_ptr++; // move through register arguments
Ian Rogers57b86d42012-03-27 16:05:41 -0700152 reg_num++;
153 } else if (reg_num == 3) {
154 arg_ptr = sp + 8; // skip to outgoing stack arguments
155 reg_num = -1;
156 } else {
Elliott Hughes74847412012-06-20 18:10:21 -0700157 DCHECK_EQ(reg_num, -1);
Ian Rogers57b86d42012-03-27 16:05:41 -0700158 arg_ptr++; // move through stack arguments
159 }
160 }
161 }
162 // Load expected destination, see Method::RegisterNative
Ian Rogers0c7abda2012-09-19 13:33:42 -0700163 const void* code = reinterpret_cast<const void*>(jni_method->GetNativeGcMap());
Ian Rogers57b86d42012-03-27 16:05:41 -0700164 if (UNLIKELY(code == NULL)) {
Jeff Hao79fe5392013-04-24 18:41:58 -0700165 code = GetJniDlsymLookupStub();
Ian Rogers57b86d42012-03-27 16:05:41 -0700166 jni_method->RegisterNative(self, code);
167 }
168 return code;
169}
170
171} // namespace art