blob: 6799159fdc49de36ede652464f13389434cdd5c1 [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 Rogers693ff612013-02-01 10:56:12 -080030extern void* FindNativeMethod(Thread* self) {
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_.
Ian Rogers693ff612013-02-01 10:56:12 -080052extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 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 Rogers693ff612013-02-01 10:56:12 -080061extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 self->DecodeJObject(to_lock)->MonitorEnter(self);
63 return JniMethodStart(self);
64}
65
66static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self) {
67 JNIEnvExt* env = self->GetJniEnv();
68 env->locals.SetSegmentState(env->local_ref_cookie);
69 env->local_ref_cookie = saved_local_ref_cookie;
70 self->PopSirt();
71}
72
Ian Rogers693ff612013-02-01 10:56:12 -080073extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 self->TransitionFromSuspendedToRunnable();
75 PopLocalReferences(saved_local_ref_cookie, self);
76}
77
78
Ian Rogers693ff612013-02-01 10:56:12 -080079extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
80 Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 self->TransitionFromSuspendedToRunnable();
82 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
83 PopLocalReferences(saved_local_ref_cookie, self);
84}
85
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080087 Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 self->TransitionFromSuspendedToRunnable();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 mirror::Object* o = self->DecodeJObject(result); // Must decode before pop.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090 PopLocalReferences(saved_local_ref_cookie, self);
91 // Process result.
92 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
93 if (self->IsExceptionPending()) {
94 return NULL;
95 }
96 CheckReferenceResult(o, self);
97 }
98 return o;
99}
100
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
102 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -0800103 jobject locked, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700104 self->TransitionFromSuspendedToRunnable();
105 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 mirror::Object* o = self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 PopLocalReferences(saved_local_ref_cookie, self);
108 // Process result.
109 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
110 if (self->IsExceptionPending()) {
111 return NULL;
112 }
113 CheckReferenceResult(o, self);
114 }
Elliott Hughesb264f082012-04-06 17:10:10 -0700115 return o;
Ian Rogers57b86d42012-03-27 16:05:41 -0700116}
117
118static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
119 intptr_t value = *arg_ptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 mirror::Object** value_as_jni_rep = reinterpret_cast<mirror::Object**>(value);
121 mirror::Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
122 CHECK(Runtime::Current()->GetHeap()->IsHeapAddress(value_as_work_around_rep))
123 << value_as_work_around_rep;
Ian Rogers57b86d42012-03-27 16:05:41 -0700124 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
125}
126
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){
Ian Rogers57b86d42012-03-27 16:05:41 -0700129 DCHECK(Thread::Current() == self);
130 // TODO: this code is specific to ARM
131 // On entry the stack pointed by sp is:
132 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
133 // | LR |
134 // | R3 | arg2
135 // | R2 | arg1
136 // | R1 | jclass/jobject
137 // | R0 | JNIEnv
138 // | unused |
139 // | unused |
140 // | unused | <- sp
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141 mirror::AbstractMethod* jni_method = self->GetCurrentMethod();
Ian Rogers57b86d42012-03-27 16:05:41 -0700142 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
143 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
144 // Fix up this/jclass argument
145 WorkAroundJniBugsForJobject(arg_ptr);
146 arg_ptr++;
147 // Fix up jobject arguments
148 MethodHelper mh(jni_method);
149 int reg_num = 2; // Current register being processed, -1 for stack arguments.
150 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
151 char shorty_char = mh.GetShorty()[i];
152 if (shorty_char == 'L') {
153 WorkAroundJniBugsForJobject(arg_ptr);
154 }
155 if (shorty_char == 'J' || shorty_char == 'D') {
156 if (reg_num == 2) {
157 arg_ptr = sp + 8; // skip to out arguments
158 reg_num = -1;
159 } else if (reg_num == 3) {
160 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
161 reg_num = -1;
162 } else {
Elliott Hughes74847412012-06-20 18:10:21 -0700163 DCHECK_EQ(reg_num, -1);
Ian Rogers57b86d42012-03-27 16:05:41 -0700164 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
165 arg_ptr += 3; // unaligned, pad and move through stack arguments
166 } else {
167 arg_ptr += 2; // aligned, move through stack arguments
168 }
169 }
170 } else {
171 if (reg_num == 2) {
172 arg_ptr++; // move through register arguments
173 reg_num++;
174 } else if (reg_num == 3) {
175 arg_ptr = sp + 8; // skip to outgoing stack arguments
176 reg_num = -1;
177 } else {
Elliott Hughes74847412012-06-20 18:10:21 -0700178 DCHECK_EQ(reg_num, -1);
Ian Rogers57b86d42012-03-27 16:05:41 -0700179 arg_ptr++; // move through stack arguments
180 }
181 }
182 }
183 // Load expected destination, see Method::RegisterNative
Ian Rogers0c7abda2012-09-19 13:33:42 -0700184 const void* code = reinterpret_cast<const void*>(jni_method->GetNativeGcMap());
Ian Rogers57b86d42012-03-27 16:05:41 -0700185 if (UNLIKELY(code == NULL)) {
186 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
187 jni_method->RegisterNative(self, code);
188 }
189 return code;
190}
191
192} // namespace art