blob: 972779d17874f2a5d97213d53501fd9df6d1c483 [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
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,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046 Thread* self, byte* stack_args)
47 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070048 // Register the top of the managed stack
jeffhao5d05c752012-05-23 10:57:48 -070049 Method** proxy_sp = reinterpret_cast<Method**>(stack_args - SP_OFFSET_IN_BYTES);
Ian Rogers57b86d42012-03-27 16:05:41 -070050 DCHECK_EQ(*proxy_sp, proxy_method);
51 self->SetTopOfStack(proxy_sp, 0);
jeffhaod66a8752012-05-22 15:30:16 -070052 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), FRAME_SIZE_IN_BYTES);
Ian Rogers57b86d42012-03-27 16:05:41 -070053 // Start new JNI local reference state
54 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 ScopedObjectAccessUnchecked soa(env);
Ian Rogers57b86d42012-03-27 16:05:41 -070056 ScopedJniEnvLocalRefState env_state(env);
57 // Create local ref. copies of proxy method and the receiver
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
59 jobject proxy_method_jobj = soa.AddLocalReference<jobject>(proxy_method);
Ian Rogers57b86d42012-03-27 16:05:41 -070060
61 // Placing into local references incoming arguments from the caller's register arguments,
62 // replacing original Object* with jobject
63 MethodHelper proxy_mh(proxy_method);
64 const size_t num_params = proxy_mh.NumArgs();
65 size_t args_in_regs = 0;
66 for (size_t i = 1; i < num_params; i++) { // skip receiver
67 args_in_regs = args_in_regs + (proxy_mh.IsParamALongOrDouble(i) ? 2 : 1);
68 if (args_in_regs > 2) {
69 args_in_regs = 2;
70 break;
71 }
72 }
73 size_t cur_arg = 0; // current stack location to read
74 size_t param_index = 1; // skip receiver
75 while (cur_arg < args_in_regs && param_index < num_params) {
76 if (proxy_mh.IsParamAReference(param_index)) {
77 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 jobject jobj = soa.AddLocalReference<jobject>(obj);
Ian Rogers57b86d42012-03-27 16:05:41 -070079 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
80 }
81 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
82 param_index++;
83 }
84 // Placing into local references incoming arguments from the caller's stack arguments
jeffhao5d05c752012-05-23 10:57:48 -070085 cur_arg += ARG2_OFFSET_IN_WORDS;
Ian Rogers57b86d42012-03-27 16:05:41 -070086 while (param_index < num_params) {
87 if (proxy_mh.IsParamAReference(param_index)) {
88 Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089 jobject jobj = soa.AddLocalReference<jobject>(obj);
Ian Rogers57b86d42012-03-27 16:05:41 -070090 *reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)) = jobj;
91 }
92 cur_arg = cur_arg + (proxy_mh.IsParamALongOrDouble(param_index) ? 2 : 1);
93 param_index++;
94 }
95 // Set up arguments array and place in local IRT during boxing (which may allocate/GC)
96 jvalue args_jobj[3];
97 args_jobj[0].l = rcvr_jobj;
98 args_jobj[1].l = proxy_method_jobj;
99 // Args array, if no arguments then NULL (don't include receiver in argument count)
100 args_jobj[2].l = NULL;
101 ObjectArray<Object>* args = NULL;
102 if ((num_params - 1) > 0) {
103 args = Runtime::Current()->GetClassLinker()->AllocObjectArray<Object>(num_params - 1);
104 if (args == NULL) {
105 CHECK(self->IsExceptionPending());
106 return;
107 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700108 args_jobj[2].l = soa.AddLocalReference<jobjectArray>(args);
Ian Rogers57b86d42012-03-27 16:05:41 -0700109 }
110 // Convert proxy method into expected interface method
111 Method* interface_method = proxy_method->FindOverriddenMethod();
112 DCHECK(interface_method != NULL);
113 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 args_jobj[1].l = soa.AddLocalReference<jobject>(interface_method);
Ian Rogers57b86d42012-03-27 16:05:41 -0700115 // Box arguments
116 cur_arg = 0; // reset stack location to read to start
117 // reset index, will index into param type array which doesn't include the receiver
118 param_index = 0;
119 ObjectArray<Class>* param_types = proxy_mh.GetParameterTypes();
120 if (param_types == NULL) {
121 CHECK(self->IsExceptionPending());
122 return;
123 }
124 // Check number of parameter types agrees with number from the Method - less 1 for the receiver.
125 DCHECK_EQ(static_cast<size_t>(param_types->GetLength()), num_params - 1);
126 while (cur_arg < args_in_regs && param_index < (num_params - 1)) {
127 Class* param_type = param_types->Get(param_index);
128 Object* obj;
129 if (!param_type->IsPrimitive()) {
130 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
131 } else {
132 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
133 if (cur_arg == 1 && (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble())) {
134 // long/double split over regs and stack, mask in high half from stack arguments
jeffhao5d05c752012-05-23 10:57:48 -0700135 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + ((ARG2_OFFSET_IN_WORDS + 2) * kPointerSize));
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700136 val.SetJ((val.GetJ() & 0xffffffffULL) | (high_half << 32));
Ian Rogers57b86d42012-03-27 16:05:41 -0700137 }
138 BoxPrimitive(param_type->GetPrimitiveType(), val);
139 if (self->IsExceptionPending()) {
140 return;
141 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700142 obj = val.GetL();
Ian Rogers57b86d42012-03-27 16:05:41 -0700143 }
144 args->Set(param_index, obj);
145 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
146 param_index++;
147 }
148 // Placing into local references incoming arguments from the caller's stack arguments
jeffhao5d05c752012-05-23 10:57:48 -0700149 cur_arg += ARG2_OFFSET_IN_WORDS;
Ian Rogers57b86d42012-03-27 16:05:41 -0700150 while (param_index < (num_params - 1)) {
151 Class* param_type = param_types->Get(param_index);
152 Object* obj;
153 if (!param_type->IsPrimitive()) {
154 obj = self->DecodeJObject(*reinterpret_cast<jobject*>(stack_args + (cur_arg * kPointerSize)));
155 } else {
156 JValue val = *reinterpret_cast<JValue*>(stack_args + (cur_arg * kPointerSize));
157 BoxPrimitive(param_type->GetPrimitiveType(), val);
158 if (self->IsExceptionPending()) {
159 return;
160 }
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700161 obj = val.GetL();
Ian Rogers57b86d42012-03-27 16:05:41 -0700162 }
163 args->Set(param_index, obj);
164 cur_arg = cur_arg + (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble() ? 2 : 1);
165 param_index++;
166 }
167 // Get the InvocationHandler method and the field that holds it within the Proxy object
Elliott Hugheseac76672012-05-24 21:56:51 -0700168 DCHECK(env->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
169 jobject inv_hand = env->GetObjectField(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy_h);
Ian Rogers57b86d42012-03-27 16:05:41 -0700170 // Call InvocationHandler.invoke
Elliott Hugheseac76672012-05-24 21:56:51 -0700171 jobject result = env->CallObjectMethodA(inv_hand, WellKnownClasses::java_lang_reflect_InvocationHandler_invoke, args_jobj);
Ian Rogers57b86d42012-03-27 16:05:41 -0700172 // Place result in stack args
173 if (!self->IsExceptionPending()) {
174 Object* result_ref = self->DecodeJObject(result);
175 if (result_ref != NULL) {
176 JValue result_unboxed;
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700177 bool unboxed_okay = UnboxPrimitiveForResult(result_ref, proxy_mh.GetReturnType(), result_unboxed);
Ian Rogers57b86d42012-03-27 16:05:41 -0700178 if (!unboxed_okay) {
179 self->ClearException();
180 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
181 "Couldn't convert result of type %s to %s",
182 PrettyTypeOf(result_ref).c_str(),
183 PrettyDescriptor(proxy_mh.GetReturnType()).c_str());
184 return;
185 }
186 *reinterpret_cast<JValue*>(stack_args) = result_unboxed;
187 } else {
188 *reinterpret_cast<jobject*>(stack_args) = NULL;
189 }
190 } else {
191 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
192 // a UndeclaredThrowableException.
193 Throwable* exception = self->GetException();
Elliott Hughesa4f94742012-05-29 16:28:38 -0700194 if (exception->IsCheckedException()) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700195 SynthesizedProxyClass* proxy_class =
196 down_cast<SynthesizedProxyClass*>(proxy_method->GetDeclaringClass());
197 int throws_index = -1;
198 size_t num_virt_methods = proxy_class->NumVirtualMethods();
199 for (size_t i = 0; i < num_virt_methods; i++) {
200 if (proxy_class->GetVirtualMethod(i) == proxy_method) {
201 throws_index = i;
202 break;
203 }
204 }
205 CHECK_NE(throws_index, -1);
206 ObjectArray<Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
207 Class* exception_class = exception->GetClass();
208 bool declares_exception = false;
209 for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
210 Class* declared_exception = declared_exceptions->Get(i);
211 declares_exception = declared_exception->IsAssignableFrom(exception_class);
212 }
Elliott Hughesa4f94742012-05-29 16:28:38 -0700213 if (!declares_exception) {
214 self->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;", NULL);
Ian Rogers57b86d42012-03-27 16:05:41 -0700215 }
216 }
217 }
218}
219
220} // namespace art