blob: 83d2265975f92c54cc892466f23bb643b80fc3f6 [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 Rogers365c1022012-06-22 15:05:28 -070021#include "scoped_jni_thread_state.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070022#include "thread.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070023#include "well_known_classes.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070024
25#include "ScopedLocalRef.h"
26
jeffhaod66a8752012-05-22 15:30:16 -070027#if defined(__arm__)
jeffhao5d05c752012-05-23 10:57:48 -070028#define SP_OFFSET_IN_BYTES 12
jeffhaod66a8752012-05-22 15:30:16 -070029#define FRAME_SIZE_IN_BYTES 48u
jeffhao5d05c752012-05-23 10:57:48 -070030#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 -070031#elif defined(__i386__)
jeffhao5d05c752012-05-23 10:57:48 -070032#define SP_OFFSET_IN_BYTES 8
jeffhaod66a8752012-05-22 15:30:16 -070033#define FRAME_SIZE_IN_BYTES 32u
jeffhao5d05c752012-05-23 10:57:48 -070034#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 -070035#else
jeffhao5d05c752012-05-23 10:57:48 -070036#error "Unsupported architecture"
jeffhaod66a8752012-05-22 15:30:16 -070037#endif
38
Ian Rogers57b86d42012-03-27 16:05:41 -070039namespace art {
40
Ian Rogers57b86d42012-03-27 16:05:41 -070041// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
42// which is responsible for recording callee save registers. We explicitly handlerize incoming
43// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
44// the invocation handler which is a field within the proxy object receiver.
45extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver,
46 Thread* self, byte* stack_args) {
47 // Register the top of the managed stack
jeffhao5d05c752012-05-23 10:57:48 -070048 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - SP_OFFSET_IN_BYTES);
Ian Rogers57b86d42012-03-27 16:05:41 -070049 DCHECK_EQ(*proxy_sp, proxy_method);
50 self->SetTopOfStack(proxy_sp, 0);
jeffhaod66a8752012-05-22 15:30:16 -070051 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), FRAME_SIZE_IN_BYTES);
Ian Rogers57b86d42012-03-27 16:05:41 -070052 // Start new JNI local reference state
53 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers365c1022012-06-22 15:05:28 -070054 ScopedJniThreadState ts(env);
Ian Rogers57b86d42012-03-27 16:05:41 -070055 ScopedJniEnvLocalRefState env_state(env);
56 // Create local ref. copies of proxy method and the receiver
Ian Rogers365c1022012-06-22 15:05:28 -070057 jobject rcvr_jobj = ts.AddLocalReference<jobject>(receiver);
58 jobject proxy_method_jobj = ts.AddLocalReference<jobject>(proxy_method);
Ian Rogers57b86d42012-03-27 16:05:41 -070059
60 // Placing into local references incoming arguments from the caller's register arguments,
61 // replacing original Object* with jobject
62 MethodHelper proxy_mh(proxy_method);
63 const size_t num_params = proxy_mh.NumArgs();
64 size_t args_in_regs = 0;
65 for (size_t i = 1; i < num_params; i++) { // skip receiver
66 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
67 if (args_in_regs > 2) {
68 args_in_regs = 2;
69 break;
70 }
71 }
72 size_t cur_arg = 0; // current stack location to read
73 size_t param_index = 1; // skip receiver
74 while (cur_arg < args_in_regs && param_index < num_params) {
75 if (proxy_mh.IsParamAReference(param_index)) {
76 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogers365c1022012-06-22 15:05:28 -070077 jobject jobj = ts.AddLocalReference<jobject>(obj);
Ian Rogers57b86d42012-03-27 16:05:41 -070078 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
79 }
80 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
81 param_index++;
82 }
83 // Placing into local references incoming arguments from the caller's stack arguments
jeffhao5d05c752012-05-23 10:57:48 -070084 cur_arg += ARG2_OFFSET_IN_WORDS;
Ian Rogers57b86d42012-03-27 16:05:41 -070085 while (param_index < num_params) {
86 if (proxy_mh.IsParamAReference(param_index)) {
87 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogers365c1022012-06-22 15:05:28 -070088 jobject jobj = ts.AddLocalReference<jobject>(obj);
Ian Rogers57b86d42012-03-27 16:05:41 -070089 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
90 }
91 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
92 param_index++;
93 }
94 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
95 jvalue args_jobj[3];
96 args_jobj[0].l = rcvr_jobj;
97 args_jobj[1].l = proxy_method_jobj;
98 // Args array, if no arguments then NULL (don't include receiver in argument count)
99 args_jobj[2].l = NULL;
100 ObjectArray<Object>* args = NULL;
101 if ((num_params - 1) > 0) {
102 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
103 if (args == NULL) {
104 CHECK(self->IsExceptionPending());
105 return;
106 }
Ian Rogers365c1022012-06-22 15:05:28 -0700107 args_jobj[2].l = ts.AddLocalReference<jobjectArray>(args);
Ian Rogers57b86d42012-03-27 16:05:41 -0700108 }
109 // Convert proxy method into expected interface method
110 Method* interface_method = proxy_method->FindOverriddenMethod();
111 DCHECK(interface_method != NULL);
112 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers365c1022012-06-22 15:05:28 -0700113 args_jobj[1].l = ts.AddLocalReference<jobject>(interface_method);
Ian Rogers57b86d42012-03-27 16:05:41 -0700114 // Box arguments
115 cur_arg = 0; // reset stack location to read to start
116 // reset index, will index into param type array which doesn't include the receiver
117 param_index = 0;
118 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
119 if (param_types == NULL) {
120 CHECK(self->IsExceptionPending());
121 return;
122 }
123 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
124 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
125 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
126 Class* param_type = param_types->Get(param_index);
127 Object* obj;
128 if (!param_type->IsPrimitive()) {
129 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
130 } else {
131 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
132 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
133 // long/double split over regs and stack, mask in high half from stack arguments
jeffhao5d05c752012-05-23 10:57:48 -0700134 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + ((ARG2_OFFSET_IN_WORDS + 2) * kPointerSize));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700135 val.SetJ((val.GetJ() & 0xffffffffULL) | (high_half << 32));
Ian Rogers57b86d42012-03-27 16:05:41 -0700136 }
137 BoxPrimitive(param_type->GetPrimitiveType(), val);
138 if (self->IsExceptionPending()) {
139 return;
140 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700141 obj = val.GetL();
Ian Rogers57b86d42012-03-27 16:05:41 -0700142 }
143 args->Set(param_index, obj);
144 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
145 param_index++;
146 }
147 // Placing into local references incoming arguments from the caller's stack arguments
jeffhao5d05c752012-05-23 10:57:48 -0700148 cur_arg += ARG2_OFFSET_IN_WORDS;
Ian Rogers57b86d42012-03-27 16:05:41 -0700149 while (param_index < (num_params - 1)) {
150 Class* param_type = param_types->Get(param_index);
151 Object* obj;
152 if (!param_type->IsPrimitive()) {
153 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
154 } else {
155 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
156 BoxPrimitive(param_type->GetPrimitiveType(), val);
157 if (self->IsExceptionPending()) {
158 return;
159 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700160 obj = val.GetL();
Ian Rogers57b86d42012-03-27 16:05:41 -0700161 }
162 args->Set(param_index, obj);
163 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
164 param_index++;
165 }
166 // Get the InvocationHandler method and the field that holds it within the Proxy object
Elliott Hugheseac76672012-05-24 21:56:51 -0700167 DCHECK(env->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
168 jobject inv_hand = env->GetObjectField(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy_h);
Ian Rogers57b86d42012-03-27 16:05:41 -0700169 // Call InvocationHandler.invoke
Elliott Hugheseac76672012-05-24 21:56:51 -0700170 jobject result = env->CallObjectMethodA(inv_hand, WellKnownClasses::java_lang_reflect_InvocationHandler_invoke, args_jobj);
Ian Rogers57b86d42012-03-27 16:05:41 -0700171 // Place result in stack args
172 if (!self->IsExceptionPending()) {
173 Object* result_ref = self->DecodeJObject(result);
174 if (result_ref != NULL) {
175 JValue result_unboxed;
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700176 bool unboxed_okay = UnboxPrimitiveForResult(result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogers57b86d42012-03-27 16:05:41 -0700177 if (!unboxed_okay) {
178 self->ClearException();
179 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
180 "Couldn't convert result of type %s to %s",
181 PrettyTypeOf(result_ref).c_str(),
182 PrettyDescriptor(proxy_mh.GetReturnType()).c_str());
183 return;
184 }
185 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
186 } else {
187 *reinterpret_cast<jobject*>(stack_args) = NULL;
188 }
189 } else {
190 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
191 // a UndeclaredThrowableException.
192 Throwable* exception = self->GetException();
Elliott Hughesa4f94742012-05-29 16:28:38 -0700193 if (exception->IsCheckedException()) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700194 SynthesizedProxyClass* proxy_class =
195 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
196 int throws_index = -1;
197 size_t num_virt_methods = proxy_class->NumVirtualMethods();
198 for (size_t i = 0; i < num_virt_methods; i++) {
199 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
200 throws_index = i;
201 break;
202 }
203 }
204 CHECK_NE(throws_index, -1);
205 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
206 Class* exception_class = exception->GetClass();
207 bool declares_exception = false;
208 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
209 Class* declared_exception = declared_exceptions->Get(i);
210 declares_exception = declared_exception->IsAssignableFrom(exception_class);
211 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700212 if (!declares_exception) {
213 self->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;", NULL);
Ian Rogers57b86d42012-03-27 16:05:41 -0700214 }
215 }
216 }
217}
218
219} // namespace art