blob: e1ae530b9c122d0d88f36c4ee97871e840bf4c81 [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
17#include "object.h"
18#include "object_utils.h"
TDYa1273d71d802012-08-15 03:47:03 -070019#include "runtime_support.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070020#include "scoped_thread_state_change.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070021#include "thread.h"
22
23namespace art {
24
25// Used by the JNI dlsym stub to find the native method to invoke if none is registered.
Ian Rogersb726dcb2012-09-05 08:57:23 -070026extern void* FindNativeMethod(Thread* self) LOCKS_EXCLUDED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -070027 Locks::mutator_lock_->AssertNotHeld(self); // We come here as Native.
Ian Rogers57b86d42012-03-27 16:05:41 -070028 DCHECK(Thread::Current() == self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029 ScopedObjectAccess soa(self);
Ian Rogers57b86d42012-03-27 16:05:41 -070030
Mathieu Chartier66f19252012-09-18 08:57:04 -070031 AbstractMethod* method = self->GetCurrentMethod();
Ian Rogers57b86d42012-03-27 16:05:41 -070032 DCHECK(method != NULL);
33
34 // Lookup symbol address for method, on failure we'll return NULL with an
35 // exception set, otherwise we return the address of the method we found.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 void* native_code = soa.Vm()->FindCodeForNativeMethod(method);
Ian Rogers57b86d42012-03-27 16:05:41 -070037 if (native_code == NULL) {
38 DCHECK(self->IsExceptionPending());
39 return NULL;
40 } else {
41 // Register so that future calls don't come here
42 method->RegisterNative(self, native_code);
43 return native_code;
44 }
45}
46
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
48extern uint32_t JniMethodStart(Thread* self) UNLOCK_FUNCTION(GlobalSynchronizatio::mutator_lock_) {
49 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers120f1c72012-09-28 17:17:10 -070050 DCHECK(env != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
52 env->local_ref_cookie = env->locals.GetSegmentState();
53 self->TransitionFromRunnableToSuspended(kNative);
54 return saved_local_ref_cookie;
55}
Elliott Hughesb264f082012-04-06 17:10:10 -070056
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -070058 UNLOCK_FUNCTION(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 self->DecodeJObject(to_lock)->MonitorEnter(self);
60 return JniMethodStart(self);
61}
62
63static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self) {
64 JNIEnvExt* env = self->GetJniEnv();
65 env->locals.SetSegmentState(env->local_ref_cookie);
66 env->local_ref_cookie = saved_local_ref_cookie;
67 self->PopSirt();
68}
69
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -070071 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072 self->TransitionFromSuspendedToRunnable();
73 PopLocalReferences(saved_local_ref_cookie, self);
74}
75
76
77extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -070078 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079 self->TransitionFromSuspendedToRunnable();
80 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
81 PopLocalReferences(saved_local_ref_cookie, self);
82}
83
84extern Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
85 Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -070086 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 self->TransitionFromSuspendedToRunnable();
88 Object* o = self->DecodeJObject(result); // Must decode before pop.
89 PopLocalReferences(saved_local_ref_cookie, self);
90 // Process result.
91 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
92 if (self->IsExceptionPending()) {
93 return NULL;
94 }
95 CheckReferenceResult(o, self);
96 }
97 return o;
98}
99
100extern Object* JniMethodEndWithReferenceSynchronized(jobject result,
101 uint32_t saved_local_ref_cookie,
102 jobject locked, Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700103 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700104 self->TransitionFromSuspendedToRunnable();
105 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
106 Object* o = self->DecodeJObject(result);
107 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;
120 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
121 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)) << value_as_work_around_rep;
123 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
124}
125
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_){
Ian Rogers57b86d42012-03-27 16:05:41 -0700128 DCHECK(Thread::Current() == self);
129 // TODO: this code is specific to ARM
130 // On entry the stack pointed by sp is:
131 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
132 // | LR |
133 // | R3 | arg2
134 // | R2 | arg1
135 // | R1 | jclass/jobject
136 // | R0 | JNIEnv
137 // | unused |
138 // | unused |
139 // | unused | <- sp
Mathieu Chartier66f19252012-09-18 08:57:04 -0700140 AbstractMethod* jni_method = self->GetCurrentMethod();
Ian Rogers57b86d42012-03-27 16:05:41 -0700141 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
142 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
143 // Fix up this/jclass argument
144 WorkAroundJniBugsForJobject(arg_ptr);
145 arg_ptr++;
146 // Fix up jobject arguments
147 MethodHelper mh(jni_method);
148 int reg_num = 2; // Current register being processed, -1 for stack arguments.
149 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
150 char shorty_char = mh.GetShorty()[i];
151 if (shorty_char == 'L') {
152 WorkAroundJniBugsForJobject(arg_ptr);
153 }
154 if (shorty_char == 'J' || shorty_char == 'D') {
155 if (reg_num == 2) {
156 arg_ptr = sp + 8; // skip to out arguments
157 reg_num = -1;
158 } else if (reg_num == 3) {
159 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
160 reg_num = -1;
161 } else {
Elliott Hughes74847412012-06-20 18:10:21 -0700162 DCHECK_EQ(reg_num, -1);
Ian Rogers57b86d42012-03-27 16:05:41 -0700163 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
164 arg_ptr += 3; // unaligned, pad and move through stack arguments
165 } else {
166 arg_ptr += 2; // aligned, move through stack arguments
167 }
168 }
169 } else {
170 if (reg_num == 2) {
171 arg_ptr++; // move through register arguments
172 reg_num++;
173 } else if (reg_num == 3) {
174 arg_ptr = sp + 8; // skip to outgoing stack arguments
175 reg_num = -1;
176 } else {
Elliott Hughes74847412012-06-20 18:10:21 -0700177 DCHECK_EQ(reg_num, -1);
Ian Rogers57b86d42012-03-27 16:05:41 -0700178 arg_ptr++; // move through stack arguments
179 }
180 }
181 }
182 // Load expected destination, see Method::RegisterNative
Ian Rogers0c7abda2012-09-19 13:33:42 -0700183 const void* code = reinterpret_cast<const void*>(jni_method->GetNativeGcMap());
Ian Rogers57b86d42012-03-27 16:05:41 -0700184 if (UNLIKELY(code == NULL)) {
185 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
186 jni_method->RegisterNative(self, code);
187 }
188 return code;
189}
190
191} // namespace art