blob: 0e21700cacd2b52c14107bc18d2dc6590d64df3f [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 Rogers2dd0e2c2013-01-24 12:42:14 -080017#include "mirror/class-inl.h"
18#include "mirror/abstract_method-inl.h"
19#include "mirror/object.h"
20#include "mirror/object-inl.h"
21#include "mirror/object_array-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070022#include "object_utils.h"
TDYa1273d71d802012-08-15 03:47:03 -070023#include "runtime_support.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070025#include "thread.h"
26
27namespace art {
28
29// Used by the JNI dlsym stub to find the native method to invoke if none is registered.
Ian Rogersb726dcb2012-09-05 08:57:23 -070030extern void* FindNativeMethod(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -070031 Locks::mutator_lock_->AssertNotHeld(self); // We come here as Native.
Ian Rogers57b86d42012-03-27 16:05:41 -070032 DCHECK(Thread::Current() == self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033 ScopedObjectAccess soa(self);
Ian Rogers57b86d42012-03-27 16:05:41 -070034
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035 mirror::AbstractMethod* method = self->GetCurrentMethod();
Ian Rogers57b86d42012-03-27 16:05:41 -070036 DCHECK(method != NULL);
37
38 // Lookup symbol address for method, on failure we'll return NULL with an
39 // exception set, otherwise we return the address of the method we found.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070040 void* native_code = soa.Vm()->FindCodeForNativeMethod(method);
Ian Rogers57b86d42012-03-27 16:05:41 -070041 if (native_code == NULL) {
42 DCHECK(self->IsExceptionPending());
43 return NULL;
44 } else {
45 // Register so that future calls don't come here
46 method->RegisterNative(self, native_code);
47 return native_code;
48 }
49}
50
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
52extern uint32_t JniMethodStart(Thread* self) UNLOCK_FUNCTION(GlobalSynchronizatio::mutator_lock_) {
53 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers120f1c72012-09-28 17:17:10 -070054 DCHECK(env != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
56 env->local_ref_cookie = env->locals.GetSegmentState();
57 self->TransitionFromRunnableToSuspended(kNative);
58 return saved_local_ref_cookie;
59}
Elliott Hughesb264f082012-04-06 17:10:10 -070060
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -070062 UNLOCK_FUNCTION(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 self->DecodeJObject(to_lock)->MonitorEnter(self);
64 return JniMethodStart(self);
65}
66
67static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self) {
68 JNIEnvExt* env = self->GetJniEnv();
69 env->locals.SetSegmentState(env->local_ref_cookie);
70 env->local_ref_cookie = saved_local_ref_cookie;
71 self->PopSirt();
72}
73
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -070075 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 self->TransitionFromSuspendedToRunnable();
77 PopLocalReferences(saved_local_ref_cookie, self);
78}
79
80
81extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -070082 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 self->TransitionFromSuspendedToRunnable();
84 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
85 PopLocalReferences(saved_local_ref_cookie, self);
86}
87
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
89 Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -070090 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 self->TransitionFromSuspendedToRunnable();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 mirror::Object* o = self->DecodeJObject(result); // Must decode before pop.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 PopLocalReferences(saved_local_ref_cookie, self);
94 // Process result.
95 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
96 if (self->IsExceptionPending()) {
97 return NULL;
98 }
99 CheckReferenceResult(o, self);
100 }
101 return o;
102}
103
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
105 uint32_t saved_local_ref_cookie,
106 jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700107 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108 self->TransitionFromSuspendedToRunnable();
109 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 mirror::Object* o = self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111 PopLocalReferences(saved_local_ref_cookie, self);
112 // Process result.
113 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
114 if (self->IsExceptionPending()) {
115 return NULL;
116 }
117 CheckReferenceResult(o, self);
118 }
Elliott Hughesb264f082012-04-06 17:10:10 -0700119 return o;
Ian Rogers57b86d42012-03-27 16:05:41 -0700120}
121
122static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
123 intptr_t value = *arg_ptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124 mirror::Object** value_as_jni_rep = reinterpret_cast<mirror::Object**>(value);
125 mirror::Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
126 CHECK(Runtime::Current()->GetHeap()->IsHeapAddress(value_as_work_around_rep))
127 << value_as_work_around_rep;
Ian Rogers57b86d42012-03-27 16:05:41 -0700128 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
129}
130
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700132 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){
Ian Rogers57b86d42012-03-27 16:05:41 -0700133 DCHECK(Thread::Current() == self);
134 // TODO: this code is specific to ARM
135 // On entry the stack pointed by sp is:
136 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
137 // | LR |
138 // | R3 | arg2
139 // | R2 | arg1
140 // | R1 | jclass/jobject
141 // | R0 | JNIEnv
142 // | unused |
143 // | unused |
144 // | unused | <- sp
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 mirror::AbstractMethod* jni_method = self->GetCurrentMethod();
Ian Rogers57b86d42012-03-27 16:05:41 -0700146 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
147 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
148 // Fix up this/jclass argument
149 WorkAroundJniBugsForJobject(arg_ptr);
150 arg_ptr++;
151 // Fix up jobject arguments
152 MethodHelper mh(jni_method);
153 int reg_num = 2; // Current register being processed, -1 for stack arguments.
154 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
155 char shorty_char = mh.GetShorty()[i];
156 if (shorty_char == 'L') {
157 WorkAroundJniBugsForJobject(arg_ptr);
158 }
159 if (shorty_char == 'J' || shorty_char == 'D') {
160 if (reg_num == 2) {
161 arg_ptr = sp + 8; // skip to out arguments
162 reg_num = -1;
163 } else if (reg_num == 3) {
164 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
165 reg_num = -1;
166 } else {
Elliott Hughes74847412012-06-20 18:10:21 -0700167 DCHECK_EQ(reg_num, -1);
Ian Rogers57b86d42012-03-27 16:05:41 -0700168 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
169 arg_ptr += 3; // unaligned, pad and move through stack arguments
170 } else {
171 arg_ptr += 2; // aligned, move through stack arguments
172 }
173 }
174 } else {
175 if (reg_num == 2) {
176 arg_ptr++; // move through register arguments
177 reg_num++;
178 } else if (reg_num == 3) {
179 arg_ptr = sp + 8; // skip to outgoing stack arguments
180 reg_num = -1;
181 } else {
Elliott Hughes74847412012-06-20 18:10:21 -0700182 DCHECK_EQ(reg_num, -1);
Ian Rogers57b86d42012-03-27 16:05:41 -0700183 arg_ptr++; // move through stack arguments
184 }
185 }
186 }
187 // Load expected destination, see Method::RegisterNative
Ian Rogers0c7abda2012-09-19 13:33:42 -0700188 const void* code = reinterpret_cast<const void*>(jni_method->GetNativeGcMap());
Ian Rogers57b86d42012-03-27 16:05:41 -0700189 if (UNLIKELY(code == NULL)) {
190 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
191 jni_method->RegisterNative(self, code);
192 }
193 return code;
194}
195
196} // namespace art