blob: 7ece7848d63c33cf3851c7f13807912613826e07 [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 Rogers00f7d0e2012-07-19 15:28:27 -070021#include "scoped_thread_state_change.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
jeffhao7fbee072012-08-24 17:56:54 -070031#elif defined(__mips__)
32#define SP_OFFSET_IN_BYTES 8
jeffhao30a33172012-10-22 18:16:22 -070033#define FRAME_SIZE_IN_BYTES 48u
34#define ARG2_OFFSET_IN_WORDS 12 // offset to 3rd arg; skip callee saves, LR, Method* and out arg spills for OUT0 to OUT2
jeffhaod66a8752012-05-22 15:30:16 -070035#elif defined(__i386__)
jeffhao5d05c752012-05-23 10:57:48 -070036#define SP_OFFSET_IN_BYTES 8
jeffhaod66a8752012-05-22 15:30:16 -070037#define FRAME_SIZE_IN_BYTES 32u
jeffhao5d05c752012-05-23 10:57:48 -070038#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 -070039#else
jeffhao5d05c752012-05-23 10:57:48 -070040#error "Unsupported architecture"
jeffhaod66a8752012-05-22 15:30:16 -070041#endif
42
Ian Rogers57b86d42012-03-27 16:05:41 -070043namespace art {
44
Ian Rogers57b86d42012-03-27 16:05:41 -070045// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
46// which is responsible for recording callee save registers. We explicitly handlerize incoming
47// reference arguments (so they survive GC) and create a boxed argument array. Finally we invoke
48// the invocation handler which is a field within the proxy object receiver.
Mathieu Chartier66f19252012-09-18 08:57:04 -070049extern "C" void artProxyInvokeHandler(AbstractMethod* proxy_method, Object* receiver,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 Thread* self, byte* stack_args)
Ian Rogersb726dcb2012-09-05 08:57:23 -070051 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070052 // Register the top of the managed stack
Mathieu Chartier66f19252012-09-18 08:57:04 -070053 AbstractMethod** proxy_sp = reinterpret_cast<AbstractMethod**>(stack_args - SP_OFFSET_IN_BYTES);
Ian Rogers57b86d42012-03-27 16:05:41 -070054 DCHECK_EQ(*proxy_sp, proxy_method);
55 self->SetTopOfStack(proxy_sp, 0);
jeffhaod66a8752012-05-22 15:30:16 -070056 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), FRAME_SIZE_IN_BYTES);
Ian Rogers57b86d42012-03-27 16:05:41 -070057 // Start new JNI local reference state
58 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 ScopedObjectAccessUnchecked soa(env);
Ian Rogers57b86d42012-03-27 16:05:41 -070060 ScopedJniEnvLocalRefState env_state(env);
61 // Create local ref. copies of proxy method and the receiver
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
63 jobject proxy_method_jobj = soa.AddLocalReference<jobject>(proxy_method);
Ian Rogers57b86d42012-03-27 16:05:41 -070064
65 // Placing into local references incoming arguments from the caller's register arguments,
66 // replacing original Object* with jobject
67 MethodHelper proxy_mh(proxy_method);
68 const size_t num_params = proxy_mh.NumArgs();
69 size_t args_in_regs = 0;
70 for (size_t i = 1; i < num_params; i++) { // skip receiver
71 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
72 if (args_in_regs > 2) {
73 args_in_regs = 2;
74 break;
75 }
76 }
77 size_t cur_arg = 0; // current stack location to read
78 size_t param_index = 1; // skip receiver
79 while (cur_arg < args_in_regs && param_index < num_params) {
80 if (proxy_mh.IsParamAReference(param_index)) {
81 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 jobject jobj = soa.AddLocalReference<jobject>(obj);
Ian Rogers57b86d42012-03-27 16:05:41 -070083 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
84 }
85 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
86 param_index++;
87 }
88 // Placing into local references incoming arguments from the caller's stack arguments
jeffhao5d05c752012-05-23 10:57:48 -070089 cur_arg += ARG2_OFFSET_IN_WORDS;
Ian Rogers57b86d42012-03-27 16:05:41 -070090 while (param_index < num_params) {
91 if (proxy_mh.IsParamAReference(param_index)) {
92 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 jobject jobj = soa.AddLocalReference<jobject>(obj);
Ian Rogers57b86d42012-03-27 16:05:41 -070094 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
95 }
96 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
97 param_index++;
98 }
99 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
100 jvalue args_jobj[3];
101 args_jobj[0].l = rcvr_jobj;
102 args_jobj[1].l = proxy_method_jobj;
103 // Args array, if no arguments then NULL (don't include receiver in argument count)
104 args_jobj[2].l = NULL;
105 ObjectArray<Object>* args = NULL;
106 if ((num_params - 1) > 0) {
Ian Rogers50b35e22012-10-04 10:09:15 -0700107 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(self, num_params - 1);
Ian Rogers57b86d42012-03-27 16:05:41 -0700108 if (args == NULL) {
109 CHECK(self->IsExceptionPending());
110 return;
111 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 args_jobj[2].l = soa.AddLocalReference<jobjectArray>(args);
Ian Rogers57b86d42012-03-27 16:05:41 -0700113 }
114 // Convert proxy method into expected interface method
Mathieu Chartier66f19252012-09-18 08:57:04 -0700115 AbstractMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogers57b86d42012-03-27 16:05:41 -0700116 DCHECK(interface_method != NULL);
117 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 args_jobj[1].l = soa.AddLocalReference<jobject>(interface_method);
Ian Rogers57b86d42012-03-27 16:05:41 -0700119 // Box arguments
120 cur_arg = 0; // reset stack location to read to start
121 // reset index, will index into param type array which doesn't include the receiver
122 param_index = 0;
Ian Rogers50b35e22012-10-04 10:09:15 -0700123 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes(self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700124 if (param_types == NULL) {
125 CHECK(self->IsExceptionPending());
126 return;
127 }
128 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
129 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
130 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
131 Class* param_type = param_types->Get(param_index);
132 Object* obj;
133 if (!param_type->IsPrimitive()) {
134 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
135 } else {
136 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
137 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
138 // long/double split over regs and stack, mask in high half from stack arguments
jeffhao5d05c752012-05-23 10:57:48 -0700139 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + ((ARG2_OFFSET_IN_WORDS + 2) * kPointerSize));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700140 val.SetJ((val.GetJ() & 0xffffffffULL) | (high_half << 32));
Ian Rogers57b86d42012-03-27 16:05:41 -0700141 }
142 BoxPrimitive(param_type->GetPrimitiveType(), val);
143 if (self->IsExceptionPending()) {
144 return;
145 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700146 obj = val.GetL();
Ian Rogers57b86d42012-03-27 16:05:41 -0700147 }
148 args->Set(param_index, obj);
149 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
150 param_index++;
151 }
152 // Placing into local references incoming arguments from the caller's stack arguments
jeffhao5d05c752012-05-23 10:57:48 -0700153 cur_arg += ARG2_OFFSET_IN_WORDS;
Ian Rogers57b86d42012-03-27 16:05:41 -0700154 while (param_index < (num_params - 1)) {
155 Class* param_type = param_types->Get(param_index);
156 Object* obj;
157 if (!param_type->IsPrimitive()) {
158 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
159 } else {
160 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
161 BoxPrimitive(param_type->GetPrimitiveType(), val);
162 if (self->IsExceptionPending()) {
163 return;
164 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700165 obj = val.GetL();
Ian Rogers57b86d42012-03-27 16:05:41 -0700166 }
167 args->Set(param_index, obj);
168 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
169 param_index++;
170 }
171 // Get the InvocationHandler method and the field that holds it within the Proxy object
Elliott Hugheseac76672012-05-24 21:56:51 -0700172 DCHECK(env->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
173 jobject inv_hand = env->GetObjectField(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy_h);
Ian Rogers57b86d42012-03-27 16:05:41 -0700174 // Call InvocationHandler.invoke
Elliott Hugheseac76672012-05-24 21:56:51 -0700175 jobject result = env->CallObjectMethodA(inv_hand, WellKnownClasses::java_lang_reflect_InvocationHandler_invoke, args_jobj);
Ian Rogers57b86d42012-03-27 16:05:41 -0700176 // Place result in stack args
177 if (!self->IsExceptionPending()) {
178 Object* result_ref = self->DecodeJObject(result);
179 if (result_ref != NULL) {
180 JValue result_unboxed;
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700181 bool unboxed_okay = UnboxPrimitiveForResult(result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogers57b86d42012-03-27 16:05:41 -0700182 if (!unboxed_okay) {
183 self->ClearException();
184 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
185 "Couldn't convert result of type %s to %s",
186 PrettyTypeOf(result_ref).c_str(),
187 PrettyDescriptor(proxy_mh.GetReturnType()).c_str());
188 return;
189 }
190 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
191 } else {
192 *reinterpret_cast<jobject*>(stack_args) = NULL;
193 }
194 } else {
195 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
196 // a UndeclaredThrowableException.
197 Throwable* exception = self->GetException();
Elliott Hughesa4f94742012-05-29 16:28:38 -0700198 if (exception->IsCheckedException()) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700199 SynthesizedProxyClass* proxy_class =
200 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
201 int throws_index = -1;
202 size_t num_virt_methods = proxy_class->NumVirtualMethods();
203 for (size_t i = 0; i < num_virt_methods; i++) {
204 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
205 throws_index = i;
206 break;
207 }
208 }
209 CHECK_NE(throws_index, -1);
210 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
211 Class* exception_class = exception->GetClass();
212 bool declares_exception = false;
213 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
214 Class* declared_exception = declared_exceptions->Get(i);
215 declares_exception = declared_exception->IsAssignableFrom(exception_class);
216 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700217 if (!declares_exception) {
218 self->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;", NULL);
Ian Rogers57b86d42012-03-27 16:05:41 -0700219 }
220 }
221 }
222}
223
224} // namespace art