Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 3 | * |
| 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" |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 20 | #include "runtime_support.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 21 | #include "scoped_thread_state_change.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 22 | #include "thread.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 23 | #include "well_known_classes.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 24 | |
| 25 | #include "ScopedLocalRef.h" |
| 26 | |
jeffhao | d66a875 | 2012-05-22 15:30:16 -0700 | [diff] [blame] | 27 | #if defined(__arm__) |
jeffhao | 5d05c75 | 2012-05-23 10:57:48 -0700 | [diff] [blame] | 28 | #define SP_OFFSET_IN_BYTES 12 |
jeffhao | d66a875 | 2012-05-22 15:30:16 -0700 | [diff] [blame] | 29 | #define FRAME_SIZE_IN_BYTES 48u |
jeffhao | 5d05c75 | 2012-05-23 10:57:48 -0700 | [diff] [blame] | 30 | #define ARG2_OFFSET_IN_WORDS 11 // offset to 3rd arg; skip callee saves, LR, Method* and out arg spills for OUT0 to OUT2 |
jeffhao | d66a875 | 2012-05-22 15:30:16 -0700 | [diff] [blame] | 31 | #elif defined(__i386__) |
jeffhao | 5d05c75 | 2012-05-23 10:57:48 -0700 | [diff] [blame] | 32 | #define SP_OFFSET_IN_BYTES 8 |
jeffhao | d66a875 | 2012-05-22 15:30:16 -0700 | [diff] [blame] | 33 | #define FRAME_SIZE_IN_BYTES 32u |
jeffhao | 5d05c75 | 2012-05-23 10:57:48 -0700 | [diff] [blame] | 34 | #define ARG2_OFFSET_IN_WORDS 8 // offset to 3rd arg; skip callee saves, LR, Method* and out arg spills for OUT0 to OUT2 |
jeffhao | d66a875 | 2012-05-22 15:30:16 -0700 | [diff] [blame] | 35 | #else |
jeffhao | 5d05c75 | 2012-05-23 10:57:48 -0700 | [diff] [blame] | 36 | #error "Unsupported architecture" |
jeffhao | d66a875 | 2012-05-22 15:30:16 -0700 | [diff] [blame] | 37 | #endif |
| 38 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 39 | namespace art { |
| 40 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 41 | // 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. |
| 45 | extern "C" void artProxyInvokeHandler(Method* proxy_method, Object* receiver, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 46 | Thread* self, byte* stack_args) |
| 47 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 48 | // Register the top of the managed stack |
jeffhao | 5d05c75 | 2012-05-23 10:57:48 -0700 | [diff] [blame] | 49 | Method** proxy_sp = reinterpret_cast<Method**>(stack_args - SP_OFFSET_IN_BYTES); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 50 | DCHECK_EQ(*proxy_sp, proxy_method); |
| 51 | self->SetTopOfStack(proxy_sp, 0); |
jeffhao | d66a875 | 2012-05-22 15:30:16 -0700 | [diff] [blame] | 52 | DCHECK_EQ(proxy_method->GetFrameSizeInBytes(), FRAME_SIZE_IN_BYTES); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 53 | // Start new JNI local reference state |
| 54 | JNIEnvExt* env = self->GetJniEnv(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 55 | ScopedObjectAccessUnchecked soa(env); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 56 | ScopedJniEnvLocalRefState env_state(env); |
| 57 | // Create local ref. copies of proxy method and the receiver |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 58 | jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver); |
| 59 | jobject proxy_method_jobj = soa.AddLocalReference<jobject>(proxy_method); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 60 | |
| 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 Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 78 | jobject jobj = soa.AddLocalReference<jobject>(obj); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 79 | *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 |
jeffhao | 5d05c75 | 2012-05-23 10:57:48 -0700 | [diff] [blame] | 85 | cur_arg += ARG2_OFFSET_IN_WORDS; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 86 | while (param_index < num_params) { |
| 87 | if (proxy_mh.IsParamAReference(param_index)) { |
| 88 | Object* obj = *reinterpret_cast<Object**>(stack_args + (cur_arg * kPointerSize)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 89 | jobject jobj = soa.AddLocalReference<jobject>(obj); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 90 | *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 Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 108 | args_jobj[2].l = soa.AddLocalReference<jobjectArray>(args); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 109 | } |
| 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 Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame^] | 114 | args_jobj[1].l = soa.AddLocalReference<jobject>(interface_method); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 115 | // 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 |
jeffhao | 5d05c75 | 2012-05-23 10:57:48 -0700 | [diff] [blame] | 135 | uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args + ((ARG2_OFFSET_IN_WORDS + 2) * kPointerSize)); |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 136 | val.SetJ((val.GetJ() & 0xffffffffULL) | (high_half << 32)); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 137 | } |
| 138 | BoxPrimitive(param_type->GetPrimitiveType(), val); |
| 139 | if (self->IsExceptionPending()) { |
| 140 | return; |
| 141 | } |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 142 | obj = val.GetL(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 143 | } |
| 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 |
jeffhao | 5d05c75 | 2012-05-23 10:57:48 -0700 | [diff] [blame] | 149 | cur_arg += ARG2_OFFSET_IN_WORDS; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 150 | 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 Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 161 | obj = val.GetL(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 162 | } |
| 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 Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 168 | 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 Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 170 | // Call InvocationHandler.invoke |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 171 | jobject result = env->CallObjectMethodA(inv_hand, WellKnownClasses::java_lang_reflect_InvocationHandler_invoke, args_jobj); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 172 | // 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 Hughes | aaa5edc | 2012-05-16 15:54:30 -0700 | [diff] [blame] | 177 | bool unboxed_okay = UnboxPrimitiveForResult(result_ref, proxy_mh.GetReturnType(), result_unboxed); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 178 | 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 Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 194 | if (exception->IsCheckedException()) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 195 | 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 Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 213 | if (!declares_exception) { |
| 214 | self->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;", NULL); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | } // namespace art |