Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -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 |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -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 | |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 17 | #include "entrypoints/entrypoint_utils.h" |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 18 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 20 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 21 | #include "gc/accounting/card_table-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 22 | #include "mirror/art_field-inl.h" |
| 23 | #include "mirror/art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/object-inl.h" |
Dragos Sbirlea | bd136a2 | 2013-08-13 18:07:04 -0700 | [diff] [blame] | 26 | #include "object_utils.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/object_array-inl.h" |
| 28 | #include "mirror/proxy.h" |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 29 | #include "reflection.h" |
| 30 | #include "scoped_thread_state_change.h" |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 31 | #include "ScopedLocalRef.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 32 | #include "well_known_classes.h" |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 33 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 34 | namespace art { |
| 35 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 36 | static inline mirror::Class* CheckFilledNewArrayAlloc(uint32_t type_idx, mirror::ArtMethod* referrer, |
| 37 | int32_t component_count, Thread* self, |
| 38 | bool access_check) |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 39 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 40 | if (UNLIKELY(component_count < 0)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 41 | ThrowNegativeArraySizeException(component_count); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 42 | return nullptr; // Failure |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 43 | } |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 44 | mirror::Class* klass = referrer->GetDexCacheResolvedTypes()->GetWithoutChecks(type_idx); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 45 | if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 46 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, referrer); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 47 | if (klass == NULL) { // Error |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 48 | DCHECK(self->IsExceptionPending()); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 49 | return nullptr; // Failure |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 50 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 51 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 52 | if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) { |
| 53 | if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 54 | ThrowRuntimeException("Bad filled array request for type %s", |
| 55 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 56 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 57 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 58 | DCHECK(throw_location.GetMethod() == referrer); |
| 59 | self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;", |
Brian Carlstrom | 4fa0bcd | 2013-12-10 11:24:21 -0800 | [diff] [blame] | 60 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 61 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 62 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 63 | return nullptr; // Failure |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 64 | } |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 65 | if (access_check) { |
| 66 | mirror::Class* referrer_klass = referrer->GetDeclaringClass(); |
| 67 | if (UNLIKELY(!referrer_klass->CanAccess(klass))) { |
| 68 | ThrowIllegalAccessErrorClass(referrer_klass, klass); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 69 | return nullptr; // Failure |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | DCHECK(klass->IsArrayClass()) << PrettyClass(klass); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 73 | return klass; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // Helper function to allocate array for FILLED_NEW_ARRAY. |
| 77 | mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* referrer, |
| 78 | int32_t component_count, Thread* self, |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 79 | bool access_check, |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 80 | gc::AllocatorType /* allocator_type */) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 81 | mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, referrer, component_count, self, |
| 82 | access_check); |
| 83 | if (UNLIKELY(klass == nullptr)) { |
| 84 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 85 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 86 | // Always go slow path for now, filled new array is not common. |
| 87 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 88 | // Use the current allocator type in case CheckFilledNewArrayAlloc caused us to suspend and then |
| 89 | // the heap switched the allocator type while we were suspended. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 90 | return mirror::Array::Alloc<false>(self, klass, component_count, klass->GetComponentSize(), |
| 91 | heap->GetCurrentAllocator()); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Helper function to allocate array for FILLED_NEW_ARRAY. |
| 95 | mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx, mirror::ArtMethod* referrer, |
| 96 | int32_t component_count, Thread* self, |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 97 | bool access_check, |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 98 | gc::AllocatorType /* allocator_type */) { |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 99 | mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, referrer, component_count, self, |
| 100 | access_check); |
| 101 | if (UNLIKELY(klass == nullptr)) { |
| 102 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 103 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 104 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 105 | // Use the current allocator type in case CheckFilledNewArrayAlloc caused us to suspend and then |
| 106 | // the heap switched the allocator type while we were suspended. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 107 | return mirror::Array::Alloc<true>(self, klass, component_count, klass->GetComponentSize(), |
| 108 | heap->GetCurrentAllocator()); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 109 | } |
| 110 | |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 111 | void ThrowStackOverflowError(Thread* self) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 112 | if (self->IsHandlingStackOverflow()) { |
| 113 | LOG(ERROR) << "Recursive stack overflow."; |
| 114 | // We don't fail here because SetStackEndForStackOverflow will print better diagnostics. |
| 115 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 116 | |
| 117 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 118 | // Remove extra entry pushed onto second stack during method tracing. |
| 119 | Runtime::Current()->GetInstrumentation()->PopMethodForUnwind(self, false); |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 120 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 121 | |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 122 | self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute. |
| 123 | JNIEnvExt* env = self->GetJniEnv(); |
| 124 | std::string msg("stack size "); |
| 125 | msg += PrettySize(self->GetStackSize()); |
| 126 | // Use low-level JNI routine and pre-baked error class to avoid class linking operations that |
| 127 | // would consume more stack. |
| 128 | int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError, |
| 129 | msg.c_str(), NULL); |
| 130 | if (rc != JNI_OK) { |
| 131 | // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME |
| 132 | // or die in the CHECK below. We may want to throw a pre-baked StackOverflowError |
| 133 | // instead. |
| 134 | LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed."; |
| 135 | CHECK(self->IsExceptionPending()); |
| 136 | } |
| 137 | self->ResetDefaultStackEnd(); // Return to default stack size. |
| 138 | } |
| 139 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 140 | JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty, |
| 141 | jobject rcvr_jobj, jobject interface_method_jobj, |
| 142 | std::vector<jvalue>& args) { |
| 143 | DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy)); |
| 144 | |
| 145 | // Build argument array possibly triggering GC. |
| 146 | soa.Self()->AssertThreadSuspensionIsAllowable(); |
| 147 | jobjectArray args_jobj = NULL; |
| 148 | const JValue zero; |
| 149 | if (args.size() > 0) { |
| 150 | args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL); |
| 151 | if (args_jobj == NULL) { |
| 152 | CHECK(soa.Self()->IsExceptionPending()); |
| 153 | return zero; |
| 154 | } |
| 155 | for (size_t i = 0; i < args.size(); ++i) { |
| 156 | if (shorty[i + 1] == 'L') { |
| 157 | jobject val = args.at(i).l; |
| 158 | soa.Env()->SetObjectArrayElement(args_jobj, i, val); |
| 159 | } else { |
| 160 | JValue jv; |
| 161 | jv.SetJ(args.at(i).j); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 162 | mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 163 | if (val == NULL) { |
| 164 | CHECK(soa.Self()->IsExceptionPending()); |
| 165 | return zero; |
| 166 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 167 | soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set<false>(i, val); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 172 | // Call Proxy.invoke(Proxy proxy, ArtMethod method, Object[] args). |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 173 | jvalue invocation_args[3]; |
| 174 | invocation_args[0].l = rcvr_jobj; |
| 175 | invocation_args[1].l = interface_method_jobj; |
| 176 | invocation_args[2].l = args_jobj; |
| 177 | jobject result = |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 178 | soa.Env()->CallStaticObjectMethodA(WellKnownClasses::java_lang_reflect_Proxy, |
| 179 | WellKnownClasses::java_lang_reflect_Proxy_invoke, |
| 180 | invocation_args); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 181 | |
| 182 | // Unbox result and handle error conditions. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 183 | if (LIKELY(!soa.Self()->IsExceptionPending())) { |
| 184 | if (shorty[0] == 'V' || (shorty[0] == 'L' && result == NULL)) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 185 | // Do nothing. |
| 186 | return zero; |
| 187 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 188 | mirror::Object* result_ref = soa.Decode<mirror::Object*>(result); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 189 | mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 190 | mirror::ArtMethod* interface_method = |
| 191 | soa.Decode<mirror::ArtMethod*>(interface_method_jobj); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 192 | mirror::Class* result_type = MethodHelper(interface_method).GetReturnType(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 193 | mirror::ArtMethod* proxy_method; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 194 | if (interface_method->GetDeclaringClass()->IsInterface()) { |
| 195 | proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(interface_method); |
| 196 | } else { |
| 197 | // Proxy dispatch to a method defined in Object. |
| 198 | DCHECK(interface_method->GetDeclaringClass()->IsObjectClass()); |
| 199 | proxy_method = interface_method; |
| 200 | } |
| 201 | ThrowLocation throw_location(rcvr, proxy_method, -1); |
| 202 | JValue result_unboxed; |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame^] | 203 | if (!UnboxPrimitiveForResult(throw_location, result_ref, result_type, &result_unboxed)) { |
Ian Rogers | 530f71c | 2013-02-22 23:29:00 -0800 | [diff] [blame] | 204 | DCHECK(soa.Self()->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 205 | return zero; |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 206 | } |
| 207 | return result_unboxed; |
| 208 | } |
| 209 | } else { |
| 210 | // In the case of checked exceptions that aren't declared, the exception must be wrapped by |
| 211 | // a UndeclaredThrowableException. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 212 | mirror::Throwable* exception = soa.Self()->GetException(NULL); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 213 | if (exception->IsCheckedException()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 214 | mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj); |
| 215 | mirror::SynthesizedProxyClass* proxy_class = |
| 216 | down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass()); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 217 | mirror::ArtMethod* interface_method = |
| 218 | soa.Decode<mirror::ArtMethod*>(interface_method_jobj); |
| 219 | mirror::ArtMethod* proxy_method = |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 220 | rcvr->GetClass()->FindVirtualMethodForInterface(interface_method); |
| 221 | int throws_index = -1; |
| 222 | size_t num_virt_methods = proxy_class->NumVirtualMethods(); |
| 223 | for (size_t i = 0; i < num_virt_methods; i++) { |
| 224 | if (proxy_class->GetVirtualMethod(i) == proxy_method) { |
| 225 | throws_index = i; |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | CHECK_NE(throws_index, -1); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 230 | mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index); |
| 231 | mirror::Class* exception_class = exception->GetClass(); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 232 | bool declares_exception = false; |
| 233 | for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 234 | mirror::Class* declared_exception = declared_exceptions->Get(i); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 235 | declares_exception = declared_exception->IsAssignableFrom(exception_class); |
| 236 | } |
| 237 | if (!declares_exception) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 238 | ThrowLocation throw_location(rcvr, proxy_method, -1); |
| 239 | soa.Self()->ThrowNewWrappedException(throw_location, |
| 240 | "Ljava/lang/reflect/UndeclaredThrowableException;", |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 241 | NULL); |
| 242 | } |
| 243 | } |
| 244 | return zero; |
| 245 | } |
| 246 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 247 | } // namespace art |