blob: e6e6478863baa3c8b27212f52e559f7c7664079f [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"
19#include "thread.h"
20
21namespace art {
22
23// Used by the JNI dlsym stub to find the native method to invoke if none is registered.
24extern void* FindNativeMethod(Thread* self) {
25 DCHECK(Thread::Current() == self);
26
27 Method* method = const_cast<Method*>(self->GetCurrentMethod());
28 DCHECK(method != NULL);
29
30 // Lookup symbol address for method, on failure we'll return NULL with an
31 // exception set, otherwise we return the address of the method we found.
32 void* native_code = self->GetJniEnv()->vm->FindCodeForNativeMethod(method);
33 if (native_code == NULL) {
34 DCHECK(self->IsExceptionPending());
35 return NULL;
36 } else {
37 // Register so that future calls don't come here
38 method->RegisterNative(self, native_code);
39 return native_code;
40 }
41}
42
43// Return value helper for jobject return types, used for JNI return values.
Elliott Hughesb264f082012-04-06 17:10:10 -070044extern Object* DecodeJObjectInThread(Thread* self, jobject java_object) {
45 if (self->IsExceptionPending()) {
Ian Rogers57b86d42012-03-27 16:05:41 -070046 return NULL;
47 }
Elliott Hughesb264f082012-04-06 17:10:10 -070048 Object* o = self->DecodeJObject(java_object);
49 if (o == NULL || !self->GetJniEnv()->check_jni) {
50 return o;
51 }
52
53 if (o == kInvalidIndirectRefObject) {
54 LOG(ERROR) << "JNI ERROR (app bug): invalid reference returned from "
55 << PrettyMethod(self->GetCurrentMethod());
56 JniAbort(NULL);
57 }
58
59 // Make sure that the result is an instance of the type this
60 // method was expected to return.
61 Method* m = self->GetCurrentMethod();
62 MethodHelper mh(m);
63 Class* return_type = mh.GetReturnType();
64
65 if (!o->InstanceOf(return_type)) {
66 LOG(ERROR) << "JNI ERROR (app bug): attempt to return an instance of " << PrettyTypeOf(o)
67 << " from " << PrettyMethod(m);
68 JniAbort(NULL);
69 }
70
71 return o;
Ian Rogers57b86d42012-03-27 16:05:41 -070072}
73
74static void WorkAroundJniBugsForJobject(intptr_t* arg_ptr) {
75 intptr_t value = *arg_ptr;
76 Object** value_as_jni_rep = reinterpret_cast<Object**>(value);
77 Object* value_as_work_around_rep = value_as_jni_rep != NULL ? *value_as_jni_rep : NULL;
78 CHECK(Runtime::Current()->GetHeap()->IsHeapAddress(value_as_work_around_rep)) << value_as_work_around_rep;
79 *arg_ptr = reinterpret_cast<intptr_t>(value_as_work_around_rep);
80}
81
82extern "C" const void* artWorkAroundAppJniBugs(Thread* self, intptr_t* sp) {
83 DCHECK(Thread::Current() == self);
84 // TODO: this code is specific to ARM
85 // On entry the stack pointed by sp is:
86 // | arg3 | <- Calling JNI method's frame (and extra bit for out args)
87 // | LR |
88 // | R3 | arg2
89 // | R2 | arg1
90 // | R1 | jclass/jobject
91 // | R0 | JNIEnv
92 // | unused |
93 // | unused |
94 // | unused | <- sp
95 Method* jni_method = self->GetTopOfStack().GetMethod();
96 DCHECK(jni_method->IsNative()) << PrettyMethod(jni_method);
97 intptr_t* arg_ptr = sp + 4; // pointer to r1 on stack
98 // Fix up this/jclass argument
99 WorkAroundJniBugsForJobject(arg_ptr);
100 arg_ptr++;
101 // Fix up jobject arguments
102 MethodHelper mh(jni_method);
103 int reg_num = 2; // Current register being processed, -1 for stack arguments.
104 for (uint32_t i = 1; i < mh.GetShortyLength(); i++) {
105 char shorty_char = mh.GetShorty()[i];
106 if (shorty_char == 'L') {
107 WorkAroundJniBugsForJobject(arg_ptr);
108 }
109 if (shorty_char == 'J' || shorty_char == 'D') {
110 if (reg_num == 2) {
111 arg_ptr = sp + 8; // skip to out arguments
112 reg_num = -1;
113 } else if (reg_num == 3) {
114 arg_ptr = sp + 10; // skip to out arguments plus 2 slots as long must be aligned
115 reg_num = -1;
116 } else {
117 DCHECK(reg_num == -1);
118 if ((reinterpret_cast<intptr_t>(arg_ptr) & 7) == 4) {
119 arg_ptr += 3; // unaligned, pad and move through stack arguments
120 } else {
121 arg_ptr += 2; // aligned, move through stack arguments
122 }
123 }
124 } else {
125 if (reg_num == 2) {
126 arg_ptr++; // move through register arguments
127 reg_num++;
128 } else if (reg_num == 3) {
129 arg_ptr = sp + 8; // skip to outgoing stack arguments
130 reg_num = -1;
131 } else {
132 DCHECK(reg_num == -1);
133 arg_ptr++; // move through stack arguments
134 }
135 }
136 }
137 // Load expected destination, see Method::RegisterNative
138 const void* code = reinterpret_cast<const void*>(jni_method->GetGcMapRaw());
139 if (UNLIKELY(code == NULL)) {
140 code = Runtime::Current()->GetJniDlsymLookupStub()->GetData();
141 jni_method->RegisterNative(self, code);
142 }
143 return code;
144}
145
146} // namespace art