blob: 25cafc296179136431ec11517b24b9f88ece667d [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
25namespace art {
26
Ian Rogers57b86d42012-03-27 16:05:41 -070027// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
28// which is responsible for recording callee save registers. We explicitly handlerize incoming
29// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
30// the invocation handler which is a field within the proxy object receiver.
31extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
32 Thread* self, byte* stack_args) {
33 // Register the top of the managed stack
34 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - 12);
35 DCHECK_EQ(*proxy_sp, proxy_method);
36 self->SetTopOfStack(proxy_sp, 0);
37 // TODO: ARM specific
38 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), 48u);
39 // Start new JNI local reference state
40 JNIEnvExt* env = self->GetJniEnv();
41 ScopedJniEnvLocalRefState env_state(env);
42 // Create local ref. copies of proxy method and the receiver
43 jobject rcvr_jobj = AddLocalReference<jobject>(env, receiver);
44 jobject proxy_method_jobj = AddLocalReference<jobject>(env, proxy_method);
45
46 // Placing into local references incoming arguments from the caller's register arguments,
47 // replacing original Object* with jobject
48 MethodHelper proxy_mh(proxy_method);
49 const size_t num_params = proxy_mh.NumArgs();
50 size_t args_in_regs = 0;
51 for (size_t i = 1; i < num_params; i++) { // skip receiver
52 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
53 if (args_in_regs > 2) {
54 args_in_regs = 2;
55 break;
56 }
57 }
58 size_t cur_arg = 0; // current stack location to read
59 size_t param_index = 1; // skip receiver
60 while (cur_arg < args_in_regs && param_index < num_params) {
61 if (proxy_mh.IsParamAReference(param_index)) {
62 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
63 jobject jobj = AddLocalReference<jobject>(env, obj);
64 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
65 }
66 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
67 param_index++;
68 }
69 // Placing into local references incoming arguments from the caller's stack arguments
70 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
71 while (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 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
81 jvalue args_jobj[3];
82 args_jobj[0].l = rcvr_jobj;
83 args_jobj[1].l = proxy_method_jobj;
84 // Args array, if no arguments then NULL (don't include receiver in argument count)
85 args_jobj[2].l = NULL;
86 ObjectArray<Object>* args = NULL;
87 if ((num_params - 1) > 0) {
88 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
89 if (args == NULL) {
90 CHECK(self->IsExceptionPending());
91 return;
92 }
93 args_jobj[2].l = AddLocalReference<jobjectArray>(env, args);
94 }
95 // Convert proxy method into expected interface method
96 Method* interface_method = proxy_method->FindOverriddenMethod();
97 DCHECK(interface_method != NULL);
98 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
99 args_jobj[1].l = AddLocalReference<jobject>(env, interface_method);
100 // Box arguments
101 cur_arg = 0; // reset stack location to read to start
102 // reset index, will index into param type array which doesn't include the receiver
103 param_index = 0;
104 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
105 if (param_types == NULL) {
106 CHECK(self->IsExceptionPending());
107 return;
108 }
109 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
110 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
111 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
112 Class* param_type = param_types->Get(param_index);
113 Object* obj;
114 if (!param_type->IsPrimitive()) {
115 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
116 } else {
117 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
118 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
119 // long/double split over regs and stack, mask in high half from stack arguments
120 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + (13 * kPointerSize));
121 val.j = (val.j & 0xffffffffULL) | (high_half << 32);
122 }
123 BoxPrimitive(param_type->GetPrimitiveType(), val);
124 if (self->IsExceptionPending()) {
125 return;
126 }
127 obj = val.l;
128 }
129 args->Set(param_index, obj);
130 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
131 param_index++;
132 }
133 // Placing into local references incoming arguments from the caller's stack arguments
134 cur_arg += 11; // skip callee saves, LR, Method* and out arg spills for R1 to R3
135 while (param_index < (num_params - 1)) {
136 Class* param_type = param_types->Get(param_index);
137 Object* obj;
138 if (!param_type->IsPrimitive()) {
139 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
140 } else {
141 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
142 BoxPrimitive(param_type->GetPrimitiveType(), val);
143 if (self->IsExceptionPending()) {
144 return;
145 }
146 obj = val.l;
147 }
148 args->Set(param_index, obj);
149 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
150 param_index++;
151 }
152 // Get the InvocationHandler method and the field that holds it within the Proxy object
153 static jmethodID inv_hand_invoke_mid = NULL;
154 static jfieldID proxy_inv_hand_fid = NULL;
155 if (proxy_inv_hand_fid == NULL) {
156 ScopedLocalRef<jclass> proxy(env, env->FindClass("java/lang/reflect/Proxy"));
157 proxy_inv_hand_fid = env->GetFieldID(proxy.get(), "h", "Ljava/lang/reflect/InvocationHandler;");
158 ScopedLocalRef<jclass> inv_hand_class(env, env->FindClass("java/lang/reflect/InvocationHandler"));
159 inv_hand_invoke_mid = env->GetMethodID(inv_hand_class.get(), "invoke",
160 "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
161 }
162 DCHECK(env->IsInstanceOf(rcvr_jobj, env->FindClass("java/lang/reflect/Proxy")));
163 jobject inv_hand = env->GetObjectField(rcvr_jobj, proxy_inv_hand_fid);
164 // Call InvocationHandler.invoke
165 jobject result = env->CallObjectMethodA(inv_hand, inv_hand_invoke_mid, args_jobj);
166 // Place result in stack args
167 if (!self->IsExceptionPending()) {
168 Object* result_ref = self->DecodeJObject(result);
169 if (result_ref != NULL) {
170 JValue result_unboxed;
171 bool unboxed_okay = UnboxPrimitive(result_ref, proxy_mh.GetReturnType(), result_unboxed, "result");
172 if (!unboxed_okay) {
173 self->ClearException();
174 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
175 "Couldn't convert result of type %s to %s",
176 PrettyTypeOf(result_ref).c_str(),
177 PrettyDescriptor(proxy_mh.GetReturnType()).c_str());
178 return;
179 }
180 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
181 } else {
182 *reinterpret_cast<jobject*>(stack_args) = NULL;
183 }
184 } else {
185 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
186 // a UndeclaredThrowableException.
187 Throwable* exception = self->GetException();
188 self->ClearException();
189 if (!exception->IsCheckedException()) {
190 self->SetException(exception);
191 } else {
192 SynthesizedProxyClass* proxy_class =
193 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
194 int throws_index = -1;
195 size_t num_virt_methods = proxy_class->NumVirtualMethods();
196 for (size_t i = 0; i < num_virt_methods; i++) {
197 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
198 throws_index = i;
199 break;
200 }
201 }
202 CHECK_NE(throws_index, -1);
203 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
204 Class* exception_class = exception->GetClass();
205 bool declares_exception = false;
206 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
207 Class* declared_exception = declared_exceptions->Get(i);
208 declares_exception = declared_exception->IsAssignableFrom(exception_class);
209 }
210 if (declares_exception) {
211 self->SetException(exception);
212 } else {
213 ThrowNewUndeclaredThrowableException(self, env, exception);
214 }
215 }
216 }
217}
218
219} // namespace art