blob: 66d0e2c93bdfc304e8410dcaf2077da0c1725516 [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 "reflection.h"
TDYa1275bb86012012-04-11 05:57:28 -070020#include "runtime_support.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070021#include "thread.h"
22
23#include "ScopedLocalRef.h"
24
jeffhaod66a8752012-05-22 15:30:16 -070025#if defined(__arm__)
jeffhao5d05c752012-05-23 10:57:48 -070026#define SP_OFFSET_IN_BYTES 12
jeffhaod66a8752012-05-22 15:30:16 -070027#define FRAME_SIZE_IN_BYTES 48u
jeffhao5d05c752012-05-23 10:57:48 -070028#define ARG2_OFFSET_IN_WORDS 11 // offset to 3rd arg; skip callee saves, LR, Method* and out arg spills for OUT0 to OUT2
jeffhaod66a8752012-05-22 15:30:16 -070029#elif defined(__i386__)
jeffhao5d05c752012-05-23 10:57:48 -070030#define SP_OFFSET_IN_BYTES 8
jeffhaod66a8752012-05-22 15:30:16 -070031#define FRAME_SIZE_IN_BYTES 32u
jeffhao5d05c752012-05-23 10:57:48 -070032#define ARG2_OFFSET_IN_WORDS 8 // offset to 3rd arg; skip callee saves, LR, Method* and out arg spills for OUT0 to OUT2
jeffhaod66a8752012-05-22 15:30:16 -070033#else
jeffhao5d05c752012-05-23 10:57:48 -070034#error "Unsupported architecture"
jeffhaod66a8752012-05-22 15:30:16 -070035#endif
36
Ian Rogers57b86d42012-03-27 16:05:41 -070037namespace art {
38
Ian Rogers57b86d42012-03-27 16:05:41 -070039// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
40// which is responsible for recording callee save registers. We explicitly handlerize incoming
41// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
42// the invocation handler which is a field within the proxy object receiver.
43extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
44 Thread* self, byte* stack_args) {
45 // Register the top of the managed stack
jeffhao5d05c752012-05-23 10:57:48 -070046 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - SP_OFFSET_IN_BYTES);
Ian Rogers57b86d42012-03-27 16:05:41 -070047 DCHECK_EQ(*proxy_sp, proxy_method);
48 self->SetTopOfStack(proxy_sp, 0);
jeffhaod66a8752012-05-22 15:30:16 -070049 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), FRAME_SIZE_IN_BYTES);
Ian Rogers57b86d42012-03-27 16:05:41 -070050 // Start new JNI local reference state
51 JNIEnvExt* env = self->GetJniEnv();
52 ScopedJniEnvLocalRefState env_state(env);
53 // Create local ref. copies of proxy method and the receiver
54 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
55 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
56
57 // Placing into local references incoming arguments from the caller's register arguments,
58 // replacing original Object* with jobject
59 MethodHelper proxy_mh(proxy_method);
60 const size_t num_params = proxy_mh.NumArgs();
61 size_t args_in_regs = 0;
62 for (size_t i = 1; i < num_params; i++) { // skip receiver
63 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
64 if (args_in_regs > 2) {
65 args_in_regs = 2;
66 break;
67 }
68 }
69 size_t cur_arg = 0; // current stack location to read
70 size_t param_index = 1; // skip receiver
71 while (cur_arg < args_in_regs && param_index < num_params) {
72 if (proxy_mh.IsParamAReference(param_index)) {
73 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
74 jobject jobj = AddLocalReference<jobject>(env, obj);
75 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
76 }
77 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
78 param_index++;
79 }
80 // Placing into local references incoming arguments from the caller's stack arguments
jeffhao5d05c752012-05-23 10:57:48 -070081 cur_arg += ARG2_OFFSET_IN_WORDS;
Ian Rogers57b86d42012-03-27 16:05:41 -070082 while (param_index < num_params) {
83 if (proxy_mh.IsParamAReference(param_index)) {
84 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
85 jobject jobj = AddLocalReference<jobject>(env, obj);
86 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
87 }
88 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
89 param_index++;
90 }
91 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
92 jvalue args_jobj[3];
93 args_jobj[0].l = rcvr_jobj;
94 args_jobj[1].l = proxy_method_jobj;
95 // Args array, if no arguments then NULL (don't include receiver in argument count)
96 args_jobj[2].l = NULL;
97 ObjectArray<Object>* args = NULL;
98 if ((num_params - 1) > 0) {
99 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
100 if (args == NULL) {
101 CHECK(self->IsExceptionPending());
102 return;
103 }
104 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
105 }
106 // Convert proxy method into expected interface method
107 Method* interface_method = proxy_method->FindOverriddenMethod();
108 DCHECK(interface_method != NULL);
109 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
110 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
111 // Box arguments
112 cur_arg = 0; // reset stack location to read to start
113 // reset index, will index into param type array which doesn't include the receiver
114 param_index = 0;
115 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
116 if (param_types == NULL) {
117 CHECK(self->IsExceptionPending());
118 return;
119 }
120 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
121 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
122 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
123 Class* param_type = param_types->Get(param_index);
124 Object* obj;
125 if (!param_type->IsPrimitive()) {
126 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
127 } else {
128 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
129 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
130 // long/double split over regs and stack, mask in high half from stack arguments
jeffhao5d05c752012-05-23 10:57:48 -0700131 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + ((ARG2_OFFSET_IN_WORDS + 2) * kPointerSize));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700132 val.SetJ((val.GetJ() & 0xffffffffULL) | (high_half << 32));
Ian Rogers57b86d42012-03-27 16:05:41 -0700133 }
134 BoxPrimitive(param_type->GetPrimitiveType(), val);
135 if (self->IsExceptionPending()) {
136 return;
137 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700138 obj = val.GetL();
Ian Rogers57b86d42012-03-27 16:05:41 -0700139 }
140 args->Set(param_index, obj);
141 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
142 param_index++;
143 }
144 // Placing into local references incoming arguments from the caller's stack arguments
jeffhao5d05c752012-05-23 10:57:48 -0700145 cur_arg += ARG2_OFFSET_IN_WORDS;
Ian Rogers57b86d42012-03-27 16:05:41 -0700146 while (param_index < (num_params - 1)) {
147 Class* param_type = param_types->Get(param_index);
148 Object* obj;
149 if (!param_type->IsPrimitive()) {
150 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
151 } else {
152 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
153 BoxPrimitive(param_type->GetPrimitiveType(), val);
154 if (self->IsExceptionPending()) {
155 return;
156 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700157 obj = val.GetL();
Ian Rogers57b86d42012-03-27 16:05:41 -0700158 }
159 args->Set(param_index, obj);
160 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
161 param_index++;
162 }
163 // Get the InvocationHandler method and the field that holds it within the Proxy object
164 static jmethodID inv_hand_invoke_mid = NULL;
165 static jfieldID proxy_inv_hand_fid = NULL;
166 if (proxy_inv_hand_fid == NULL) {
167 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
168 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
169 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
170 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
171 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
172 }
173 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
174 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
175 // Call InvocationHandler.invoke
176 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
177 // Place result in stack args
178 if (!self->IsExceptionPending()) {
179 Object* result_ref = self->DecodeJObject(result);
180 if (result_ref != NULL) {
181 JValue result_unboxed;
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700182 bool unboxed_okay = UnboxPrimitiveForResult(result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogers57b86d42012-03-27 16:05:41 -0700183 if (!unboxed_okay) {
184 self->ClearException();
185 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
186 "Couldn't convert result of type %s to %s",
187 PrettyTypeOf(result_ref).c_str(),
188 PrettyDescriptor(proxy_mh.GetReturnType()).c_str());
189 return;
190 }
191 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
192 } else {
193 *reinterpret_cast<jobject*>(stack_args) = NULL;
194 }
195 } else {
196 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
197 // a UndeclaredThrowableException.
198 Throwable* exception = self->GetException();
199 self->ClearException();
200 if (!exception->IsCheckedException()) {
201 self->SetException(exception);
202 } else {
203 SynthesizedProxyClass* proxy_class =
204 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
205 int throws_index = -1;
206 size_t num_virt_methods = proxy_class->NumVirtualMethods();
207 for (size_t i = 0; i < num_virt_methods; i++) {
208 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
209 throws_index = i;
210 break;
211 }
212 }
213 CHECK_NE(throws_index, -1);
214 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
215 Class* exception_class = exception->GetClass();
216 bool declares_exception = false;
217 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
218 Class* declared_exception = declared_exceptions->Get(i);
219 declares_exception = declared_exception->IsAssignableFrom(exception_class);
220 }
221 if (declares_exception) {
222 self->SetException(exception);
223 } else {
224 ThrowNewUndeclaredThrowableException(self, env, exception);
225 }
226 }
227 }
228}
229
230} // namespace art