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 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 19 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 20 | #include "art_method-inl.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 21 | #include "base/mutex.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 23 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 24 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 26 | #include "mirror/method.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/object-inl.h" |
| 28 | #include "mirror/object_array-inl.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 | |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 36 | static inline mirror::Class* CheckFilledNewArrayAlloc(uint32_t type_idx, |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 37 | int32_t component_count, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 38 | ArtMethod* referrer, |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 39 | Thread* self, |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 40 | bool access_check) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 41 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 42 | if (UNLIKELY(component_count < 0)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 43 | ThrowNegativeArraySizeException(component_count); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 44 | return nullptr; // Failure |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 45 | } |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 46 | mirror::Class* klass = referrer->GetDexCacheResolvedType<false>(type_idx); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 47 | if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 48 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, referrer); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 49 | if (klass == nullptr) { // Error |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 50 | DCHECK(self->IsExceptionPending()); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 51 | return nullptr; // Failure |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 52 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 53 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 54 | if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) { |
| 55 | if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 56 | ThrowRuntimeException("Bad filled array request for type %s", |
| 57 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 58 | } else { |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 59 | self->ThrowNewExceptionF( |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 60 | "Ljava/lang/InternalError;", |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 61 | "Found type %s; filled-new-array not implemented for anything but 'int'", |
| 62 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 63 | } |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 64 | return nullptr; // Failure |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 65 | } |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 66 | if (access_check) { |
| 67 | mirror::Class* referrer_klass = referrer->GetDeclaringClass(); |
| 68 | if (UNLIKELY(!referrer_klass->CanAccess(klass))) { |
| 69 | ThrowIllegalAccessErrorClass(referrer_klass, klass); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 70 | return nullptr; // Failure |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | DCHECK(klass->IsArrayClass()) << PrettyClass(klass); |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 74 | return klass; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Helper function to allocate array for FILLED_NEW_ARRAY. |
Andreas Gampe | 1cc7dba | 2014-12-17 18:43:01 -0800 | [diff] [blame] | 78 | mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, int32_t component_count, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 79 | ArtMethod* referrer, Thread* self, |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 80 | bool access_check, |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 81 | gc::AllocatorType /* allocator_type */) { |
Andreas Gampe | 1cc7dba | 2014-12-17 18:43:01 -0800 | [diff] [blame] | 82 | mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, component_count, referrer, self, |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 83 | access_check); |
| 84 | if (UNLIKELY(klass == nullptr)) { |
| 85 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 86 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 87 | // Always go slow path for now, filled new array is not common. |
| 88 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 89 | // Use the current allocator type in case CheckFilledNewArrayAlloc caused us to suspend and then |
| 90 | // the heap switched the allocator type while we were suspended. |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 91 | return mirror::Array::Alloc<false>(self, klass, component_count, |
| 92 | klass->GetComponentSizeShift(), |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 93 | heap->GetCurrentAllocator()); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Helper function to allocate array for FILLED_NEW_ARRAY. |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 97 | mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx, |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 98 | int32_t component_count, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 99 | ArtMethod* referrer, |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 100 | Thread* self, |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 101 | bool access_check, |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 102 | gc::AllocatorType /* allocator_type */) { |
Andreas Gampe | 1cc7dba | 2014-12-17 18:43:01 -0800 | [diff] [blame] | 103 | mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, component_count, referrer, self, |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 104 | access_check); |
| 105 | if (UNLIKELY(klass == nullptr)) { |
| 106 | return nullptr; |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 107 | } |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 108 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 109 | // Use the current allocator type in case CheckFilledNewArrayAlloc caused us to suspend and then |
| 110 | // the heap switched the allocator type while we were suspended. |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 111 | return mirror::Array::Alloc<true>(self, klass, component_count, |
| 112 | klass->GetComponentSizeShift(), |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 113 | heap->GetCurrentAllocator()); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 114 | } |
| 115 | |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 116 | void ThrowStackOverflowError(Thread* self) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 117 | if (self->IsHandlingStackOverflow()) { |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 118 | LOG(ERROR) << "Recursive stack overflow."; |
| 119 | // We don't fail here because SetStackEndForStackOverflow will print better diagnostics. |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [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()); |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 126 | |
| 127 | // Avoid running Java code for exception initialization. |
| 128 | // TODO: Checks to make this a bit less brittle. |
| 129 | |
| 130 | std::string error_msg; |
| 131 | |
| 132 | // Allocate an uninitialized object. |
| 133 | ScopedLocalRef<jobject> exc(env, |
| 134 | env->AllocObject(WellKnownClasses::java_lang_StackOverflowError)); |
| 135 | if (exc.get() != nullptr) { |
| 136 | // "Initialize". |
| 137 | // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object. |
| 138 | // Only Throwable has "custom" fields: |
| 139 | // String detailMessage. |
| 140 | // Throwable cause (= this). |
| 141 | // List<Throwable> suppressedExceptions (= Collections.emptyList()). |
| 142 | // Object stackState; |
| 143 | // StackTraceElement[] stackTrace; |
| 144 | // Only Throwable has a non-empty constructor: |
| 145 | // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT; |
| 146 | // fillInStackTrace(); |
| 147 | |
| 148 | // detailMessage. |
| 149 | // TODO: Use String::FromModifiedUTF...? |
| 150 | ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str())); |
| 151 | if (s.get() != nullptr) { |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 152 | env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get()); |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 153 | |
| 154 | // cause. |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 155 | env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get()); |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 156 | |
| 157 | // suppressedExceptions. |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 158 | ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField( |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 159 | WellKnownClasses::java_util_Collections, |
| 160 | WellKnownClasses::java_util_Collections_EMPTY_LIST)); |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 161 | CHECK(emptylist.get() != nullptr); |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 162 | env->SetObjectField(exc.get(), |
| 163 | WellKnownClasses::java_lang_Throwable_suppressedExceptions, |
| 164 | emptylist.get()); |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 165 | |
| 166 | // stackState is set as result of fillInStackTrace. fillInStackTrace calls |
| 167 | // nativeFillInStackTrace. |
| 168 | ScopedLocalRef<jobject> stack_state_val(env, nullptr); |
| 169 | { |
| 170 | ScopedObjectAccessUnchecked soa(env); |
| 171 | stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa)); |
| 172 | } |
| 173 | if (stack_state_val.get() != nullptr) { |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 174 | env->SetObjectField(exc.get(), |
| 175 | WellKnownClasses::java_lang_Throwable_stackState, |
| 176 | stack_state_val.get()); |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 177 | |
| 178 | // stackTrace. |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 179 | ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField( |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 180 | WellKnownClasses::libcore_util_EmptyArray, |
| 181 | WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT)); |
| 182 | env->SetObjectField(exc.get(), |
| 183 | WellKnownClasses::java_lang_Throwable_stackTrace, |
| 184 | stack_trace_elem.get()); |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 185 | } else { |
| 186 | error_msg = "Could not create stack trace."; |
| 187 | } |
Mathieu Chartier | 50c138f | 2015-01-07 16:00:03 -0800 | [diff] [blame] | 188 | // Throw the exception. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 189 | self->SetException(reinterpret_cast<mirror::Throwable*>(self->DecodeJObject(exc.get()))); |
Andreas Gampe | 7ea6f79 | 2014-07-14 16:21:44 -0700 | [diff] [blame] | 190 | } else { |
| 191 | // Could not allocate a string object. |
| 192 | error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed."; |
| 193 | } |
| 194 | } else { |
| 195 | error_msg = "Could not allocate StackOverflowError object."; |
| 196 | } |
| 197 | |
| 198 | if (!error_msg.empty()) { |
Andreas Gampe | 1de0f5c | 2015-02-19 10:54:31 -0800 | [diff] [blame] | 199 | LOG(WARNING) << error_msg; |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 200 | CHECK(self->IsExceptionPending()); |
| 201 | } |
Dave Allison | f943914 | 2014-03-27 15:10:22 -0700 | [diff] [blame] | 202 | |
| 203 | bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks(); |
Dave Allison | b090a18 | 2014-08-14 17:02:48 +0000 | [diff] [blame] | 204 | self->ResetDefaultStackEnd(); // Return to default stack size. |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 205 | |
| 206 | // And restore protection if implicit checks are on. |
| 207 | if (!explicit_overflow_check) { |
| 208 | self->ProtectStack(); |
| 209 | } |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Ian Rogers | e5877a1 | 2014-07-16 12:06:35 -0700 | [diff] [blame] | 212 | void CheckReferenceResult(mirror::Object* o, Thread* self) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 213 | if (o == nullptr) { |
Ian Rogers | e5877a1 | 2014-07-16 12:06:35 -0700 | [diff] [blame] | 214 | return; |
| 215 | } |
Ian Rogers | e5877a1 | 2014-07-16 12:06:35 -0700 | [diff] [blame] | 216 | // Make sure that the result is an instance of the type this method was expected to return. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 217 | mirror::Class* return_type = self->GetCurrentMethod(nullptr)->GetReturnType(); |
Ian Rogers | e5877a1 | 2014-07-16 12:06:35 -0700 | [diff] [blame] | 218 | |
| 219 | if (!o->InstanceOf(return_type)) { |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 220 | Runtime::Current()->GetJavaVM()->JniAbortF(nullptr, |
| 221 | "attempt to return an instance of %s from %s", |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 222 | PrettyTypeOf(o).c_str(), |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 223 | PrettyMethod(self->GetCurrentMethod(nullptr)).c_str()); |
Ian Rogers | e5877a1 | 2014-07-16 12:06:35 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 227 | JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty, |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 228 | jobject rcvr_jobj, jobject interface_method_jobj, |
| 229 | std::vector<jvalue>& args) { |
| 230 | DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy)); |
| 231 | |
| 232 | // Build argument array possibly triggering GC. |
| 233 | soa.Self()->AssertThreadSuspensionIsAllowable(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 234 | jobjectArray args_jobj = nullptr; |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 235 | const JValue zero; |
Jeff Hao | f00571c | 2014-05-29 17:29:47 -0700 | [diff] [blame] | 236 | int32_t target_sdk_version = Runtime::Current()->GetTargetSdkVersion(); |
| 237 | // Do not create empty arrays unless needed to maintain Dalvik bug compatibility. |
| 238 | if (args.size() > 0 || (target_sdk_version > 0 && target_sdk_version <= 21)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 239 | args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, nullptr); |
| 240 | if (args_jobj == nullptr) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 241 | CHECK(soa.Self()->IsExceptionPending()); |
| 242 | return zero; |
| 243 | } |
| 244 | for (size_t i = 0; i < args.size(); ++i) { |
| 245 | if (shorty[i + 1] == 'L') { |
| 246 | jobject val = args.at(i).l; |
| 247 | soa.Env()->SetObjectArrayElement(args_jobj, i, val); |
| 248 | } else { |
| 249 | JValue jv; |
| 250 | jv.SetJ(args.at(i).j); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 251 | mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 252 | if (val == nullptr) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 253 | CHECK(soa.Self()->IsExceptionPending()); |
| 254 | return zero; |
| 255 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 256 | soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set<false>(i, val); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 261 | // Call Proxy.invoke(Proxy proxy, Method method, Object[] args). |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 262 | jvalue invocation_args[3]; |
| 263 | invocation_args[0].l = rcvr_jobj; |
| 264 | invocation_args[1].l = interface_method_jobj; |
| 265 | invocation_args[2].l = args_jobj; |
| 266 | jobject result = |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 267 | soa.Env()->CallStaticObjectMethodA(WellKnownClasses::java_lang_reflect_Proxy, |
| 268 | WellKnownClasses::java_lang_reflect_Proxy_invoke, |
| 269 | invocation_args); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 270 | |
| 271 | // Unbox result and handle error conditions. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 272 | if (LIKELY(!soa.Self()->IsExceptionPending())) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 273 | if (shorty[0] == 'V' || (shorty[0] == 'L' && result == nullptr)) { |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 274 | // Do nothing. |
| 275 | return zero; |
| 276 | } else { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 277 | StackHandleScope<1> hs(soa.Self()); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 278 | auto h_interface_method(hs.NewHandle(soa.Decode<mirror::Method*>(interface_method_jobj))); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 279 | // This can cause thread suspension. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 280 | mirror::Class* result_type = h_interface_method->GetArtMethod()->GetReturnType(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 281 | mirror::Object* result_ref = soa.Decode<mirror::Object*>(result); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 282 | JValue result_unboxed; |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 283 | if (!UnboxPrimitiveForResult(result_ref, result_type, &result_unboxed)) { |
Ian Rogers | 530f71c | 2013-02-22 23:29:00 -0800 | [diff] [blame] | 284 | DCHECK(soa.Self()->IsExceptionPending()); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 285 | return zero; |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 286 | } |
| 287 | return result_unboxed; |
| 288 | } |
| 289 | } else { |
| 290 | // In the case of checked exceptions that aren't declared, the exception must be wrapped by |
| 291 | // a UndeclaredThrowableException. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 292 | mirror::Throwable* exception = soa.Self()->GetException(); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 293 | if (exception->IsCheckedException()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 294 | mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 295 | mirror::Class* proxy_class = rcvr->GetClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 296 | mirror::Method* interface_method = soa.Decode<mirror::Method*>(interface_method_jobj); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 297 | ArtMethod* proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface( |
| 298 | interface_method->GetArtMethod(), sizeof(void*)); |
| 299 | auto* virtual_methods = proxy_class->GetVirtualMethodsPtr(); |
| 300 | size_t num_virtuals = proxy_class->NumVirtualMethods(); |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 301 | size_t method_size = ArtMethod::Size(sizeof(void*)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 302 | int throws_index = (reinterpret_cast<uintptr_t>(proxy_method) - |
| 303 | reinterpret_cast<uintptr_t>(virtual_methods)) / method_size; |
| 304 | CHECK_LT(throws_index, static_cast<int>(num_virtuals)); |
Brian Carlstrom | 3437531 | 2014-09-10 23:10:47 -0700 | [diff] [blame] | 305 | mirror::ObjectArray<mirror::Class>* declared_exceptions = |
| 306 | proxy_class->GetThrows()->Get(throws_index); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 307 | mirror::Class* exception_class = exception->GetClass(); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 308 | bool declares_exception = false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 309 | for (int32_t i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 310 | mirror::Class* declared_exception = declared_exceptions->Get(i); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 311 | declares_exception = declared_exception->IsAssignableFrom(exception_class); |
| 312 | } |
| 313 | if (!declares_exception) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 314 | soa.Self()->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;", |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 315 | nullptr); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | return zero; |
| 319 | } |
| 320 | } |
Ian Rogers | 832336b | 2014-10-08 15:35:22 -0700 | [diff] [blame] | 321 | |
| 322 | bool FillArrayData(mirror::Object* obj, const Instruction::ArrayDataPayload* payload) { |
| 323 | DCHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature)); |
| 324 | if (UNLIKELY(obj == nullptr)) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 325 | ThrowNullPointerException("null array in FILL_ARRAY_DATA"); |
Ian Rogers | 832336b | 2014-10-08 15:35:22 -0700 | [diff] [blame] | 326 | return false; |
| 327 | } |
| 328 | mirror::Array* array = obj->AsArray(); |
| 329 | DCHECK(!array->IsObjectArray()); |
| 330 | if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) { |
| 331 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 332 | self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
Ian Rogers | 832336b | 2014-10-08 15:35:22 -0700 | [diff] [blame] | 333 | "failed FILL_ARRAY_DATA; length=%d, index=%d", |
| 334 | array->GetLength(), payload->element_count); |
| 335 | return false; |
| 336 | } |
| 337 | // Copy data from dex file to memory assuming both are little endian. |
| 338 | uint32_t size_in_bytes = payload->element_count * payload->element_width; |
| 339 | memcpy(array->GetRawData(payload->element_width, 0), payload->data, size_in_bytes); |
| 340 | return true; |
| 341 | } |
| 342 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 343 | } // namespace art |