blob: 94aced27ede8d5bf814a9601970fd802bc40fdfa [file] [log] [blame]
Shih-wei Liao2d831012011-09-28 22:06:53 -07001/*
Elliott Hughes0f3c5532012-03-30 14:51:51 -07002 * Copyright (C) 2012 The Android Open Source Project
Shih-wei Liao2d831012011-09-28 22:06:53 -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
Ian Rogers7655f292013-07-29 11:07:13 -070017#include "entrypoints/entrypoint_utils.h"
Shih-wei Liao2d831012011-09-28 22:06:53 -070018
Mathieu Chartierc7853442015-03-27 14:35:38 -070019#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070021#include "base/mutex.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070023#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070024#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070026#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/object-inl.h"
28#include "mirror/object_array-inl.h"
Ian Rogersaf6e67a2013-01-16 08:38:37 -080029#include "reflection.h"
30#include "scoped_thread_state_change.h"
TDYa1275bb86012012-04-11 05:57:28 -070031#include "ScopedLocalRef.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070032#include "well_known_classes.h"
TDYa1275bb86012012-04-11 05:57:28 -070033
jeffhao41005dd2012-05-09 17:58:52 -070034namespace art {
35
Brian Carlstrom34375312014-09-10 23:10:47 -070036static inline mirror::Class* CheckFilledNewArrayAlloc(uint32_t type_idx,
Brian Carlstrom34375312014-09-10 23:10:47 -070037 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -070038 ArtMethod* referrer,
Brian Carlstrom34375312014-09-10 23:10:47 -070039 Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080040 bool access_check)
Mathieu Chartier90443472015-07-16 20:32:27 -070041 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070042 if (UNLIKELY(component_count < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080043 ThrowNegativeArraySizeException(component_count);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080044 return nullptr; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -070045 }
Vladimir Marko05792b92015-08-03 11:56:49 +010046 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
47 size_t pointer_size = class_linker->GetImagePointerSize();
48 mirror::Class* klass = referrer->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070049 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +010050 klass = class_linker->ResolveType(type_idx, referrer);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070051 if (klass == nullptr) { // Error
Ian Rogers50b35e22012-10-04 10:09:15 -070052 DCHECK(self->IsExceptionPending());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080053 return nullptr; // Failure
Ian Rogers19846512012-02-24 11:42:47 -080054 }
Ian Rogersea2a11d2011-10-11 16:48:51 -070055 }
Ian Rogers57b86d42012-03-27 16:05:41 -070056 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
57 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Ian Rogers62d6c772013-02-27 08:32:07 -080058 ThrowRuntimeException("Bad filled array request for type %s",
59 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -080060 } else {
Brian Carlstrom34375312014-09-10 23:10:47 -070061 self->ThrowNewExceptionF(
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000062 "Ljava/lang/InternalError;",
Brian Carlstrom34375312014-09-10 23:10:47 -070063 "Found type %s; filled-new-array not implemented for anything but 'int'",
64 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -080065 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080066 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -070067 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070068 if (access_check) {
69 mirror::Class* referrer_klass = referrer->GetDeclaringClass();
70 if (UNLIKELY(!referrer_klass->CanAccess(klass))) {
71 ThrowIllegalAccessErrorClass(referrer_klass, klass);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080072 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070073 }
74 }
75 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080076 return klass;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070077}
78
79// Helper function to allocate array for FILLED_NEW_ARRAY.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080080mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -070081 ArtMethod* referrer, Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080082 bool access_check,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080083 gc::AllocatorType /* allocator_type */) {
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080084 mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, component_count, referrer, self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080085 access_check);
86 if (UNLIKELY(klass == nullptr)) {
87 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070088 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080089 // Always go slow path for now, filled new array is not common.
90 gc::Heap* heap = Runtime::Current()->GetHeap();
91 // Use the current allocator type in case CheckFilledNewArrayAlloc caused us to suspend and then
92 // the heap switched the allocator type while we were suspended.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070093 return mirror::Array::Alloc<false>(self, klass, component_count,
94 klass->GetComponentSizeShift(),
Ian Rogers6fac4472014-02-25 17:01:10 -080095 heap->GetCurrentAllocator());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070096}
97
98// Helper function to allocate array for FILLED_NEW_ARRAY.
Brian Carlstrom34375312014-09-10 23:10:47 -070099mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
Brian Carlstrom34375312014-09-10 23:10:47 -0700100 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700101 ArtMethod* referrer,
Brian Carlstrom34375312014-09-10 23:10:47 -0700102 Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800103 bool access_check,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800104 gc::AllocatorType /* allocator_type */) {
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800105 mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, component_count, referrer, self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800106 access_check);
107 if (UNLIKELY(klass == nullptr)) {
108 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700109 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800110 gc::Heap* heap = Runtime::Current()->GetHeap();
111 // Use the current allocator type in case CheckFilledNewArrayAlloc caused us to suspend and then
112 // the heap switched the allocator type while we were suspended.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700113 return mirror::Array::Alloc<true>(self, klass, component_count,
114 klass->GetComponentSizeShift(),
Ian Rogers6fac4472014-02-25 17:01:10 -0800115 heap->GetCurrentAllocator());
Ian Rogers57b86d42012-03-27 16:05:41 -0700116}
117
jeffhaod7521322012-11-21 15:38:24 -0800118void ThrowStackOverflowError(Thread* self) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700119 if (self->IsHandlingStackOverflow()) {
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700120 LOG(ERROR) << "Recursive stack overflow.";
121 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700122 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800123
jeffhaod7521322012-11-21 15:38:24 -0800124 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
125 JNIEnvExt* env = self->GetJniEnv();
126 std::string msg("stack size ");
127 msg += PrettySize(self->GetStackSize());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700128
129 // Avoid running Java code for exception initialization.
130 // TODO: Checks to make this a bit less brittle.
131
132 std::string error_msg;
133
134 // Allocate an uninitialized object.
135 ScopedLocalRef<jobject> exc(env,
136 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
137 if (exc.get() != nullptr) {
138 // "Initialize".
139 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
140 // Only Throwable has "custom" fields:
141 // String detailMessage.
142 // Throwable cause (= this).
143 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
144 // Object stackState;
145 // StackTraceElement[] stackTrace;
146 // Only Throwable has a non-empty constructor:
147 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
148 // fillInStackTrace();
149
150 // detailMessage.
151 // TODO: Use String::FromModifiedUTF...?
152 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
153 if (s.get() != nullptr) {
Brian Carlstrom34375312014-09-10 23:10:47 -0700154 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700155
156 // cause.
Brian Carlstrom34375312014-09-10 23:10:47 -0700157 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700158
159 // suppressedExceptions.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700160 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
Brian Carlstrom34375312014-09-10 23:10:47 -0700161 WellKnownClasses::java_util_Collections,
162 WellKnownClasses::java_util_Collections_EMPTY_LIST));
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700163 CHECK(emptylist.get() != nullptr);
Brian Carlstrom34375312014-09-10 23:10:47 -0700164 env->SetObjectField(exc.get(),
165 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
166 emptylist.get());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700167
168 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
169 // nativeFillInStackTrace.
170 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
171 {
172 ScopedObjectAccessUnchecked soa(env);
173 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
174 }
175 if (stack_state_val.get() != nullptr) {
Brian Carlstrom34375312014-09-10 23:10:47 -0700176 env->SetObjectField(exc.get(),
177 WellKnownClasses::java_lang_Throwable_stackState,
178 stack_state_val.get());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700179
180 // stackTrace.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700181 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
Brian Carlstrom34375312014-09-10 23:10:47 -0700182 WellKnownClasses::libcore_util_EmptyArray,
183 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
184 env->SetObjectField(exc.get(),
185 WellKnownClasses::java_lang_Throwable_stackTrace,
186 stack_trace_elem.get());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700187 } else {
188 error_msg = "Could not create stack trace.";
189 }
Mathieu Chartier50c138f2015-01-07 16:00:03 -0800190 // Throw the exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000191 self->SetException(reinterpret_cast<mirror::Throwable*>(self->DecodeJObject(exc.get())));
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700192 } else {
193 // Could not allocate a string object.
194 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
195 }
196 } else {
197 error_msg = "Could not allocate StackOverflowError object.";
198 }
199
200 if (!error_msg.empty()) {
Andreas Gampe1de0f5c2015-02-19 10:54:31 -0800201 LOG(WARNING) << error_msg;
jeffhaod7521322012-11-21 15:38:24 -0800202 CHECK(self->IsExceptionPending());
203 }
Dave Allisonf9439142014-03-27 15:10:22 -0700204
205 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
Dave Allisonb090a182014-08-14 17:02:48 +0000206 self->ResetDefaultStackEnd(); // Return to default stack size.
Dave Allison648d7112014-07-25 16:15:27 -0700207
208 // And restore protection if implicit checks are on.
209 if (!explicit_overflow_check) {
210 self->ProtectStack();
211 }
jeffhaod7521322012-11-21 15:38:24 -0800212}
213
Ian Rogerse5877a12014-07-16 12:06:35 -0700214void CheckReferenceResult(mirror::Object* o, Thread* self) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700215 if (o == nullptr) {
Ian Rogerse5877a12014-07-16 12:06:35 -0700216 return;
217 }
Ian Rogerse5877a12014-07-16 12:06:35 -0700218 // Make sure that the result is an instance of the type this method was expected to return.
Vladimir Marko05792b92015-08-03 11:56:49 +0100219 mirror::Class* return_type = self->GetCurrentMethod(nullptr)->GetReturnType(true /* resolve */,
220 sizeof(void*));
Ian Rogerse5877a12014-07-16 12:06:35 -0700221
222 if (!o->InstanceOf(return_type)) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700223 Runtime::Current()->GetJavaVM()->JniAbortF(nullptr,
224 "attempt to return an instance of %s from %s",
Ian Rogers68d8b422014-07-17 11:09:10 -0700225 PrettyTypeOf(o).c_str(),
Ian Rogersded66a02014-10-28 18:12:55 -0700226 PrettyMethod(self->GetCurrentMethod(nullptr)).c_str());
Ian Rogerse5877a12014-07-16 12:06:35 -0700227 }
228}
229
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700230JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800231 jobject rcvr_jobj, jobject interface_method_jobj,
232 std::vector<jvalue>& args) {
233 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
234
235 // Build argument array possibly triggering GC.
236 soa.Self()->AssertThreadSuspensionIsAllowable();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700237 jobjectArray args_jobj = nullptr;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800238 const JValue zero;
Jeff Haof00571c2014-05-29 17:29:47 -0700239 int32_t target_sdk_version = Runtime::Current()->GetTargetSdkVersion();
240 // Do not create empty arrays unless needed to maintain Dalvik bug compatibility.
241 if (args.size() > 0 || (target_sdk_version > 0 && target_sdk_version <= 21)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700242 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, nullptr);
243 if (args_jobj == nullptr) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800244 CHECK(soa.Self()->IsExceptionPending());
245 return zero;
246 }
247 for (size_t i = 0; i < args.size(); ++i) {
248 if (shorty[i + 1] == 'L') {
249 jobject val = args.at(i).l;
250 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
251 } else {
252 JValue jv;
253 jv.SetJ(args.at(i).j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800254 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700255 if (val == nullptr) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800256 CHECK(soa.Self()->IsExceptionPending());
257 return zero;
258 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100259 soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set<false>(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800260 }
261 }
262 }
263
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700264 // Call Proxy.invoke(Proxy proxy, Method method, Object[] args).
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800265 jvalue invocation_args[3];
266 invocation_args[0].l = rcvr_jobj;
267 invocation_args[1].l = interface_method_jobj;
268 invocation_args[2].l = args_jobj;
269 jobject result =
Brian Carlstromea46f952013-07-30 01:26:50 -0700270 soa.Env()->CallStaticObjectMethodA(WellKnownClasses::java_lang_reflect_Proxy,
271 WellKnownClasses::java_lang_reflect_Proxy_invoke,
272 invocation_args);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800273
274 // Unbox result and handle error conditions.
Ian Rogers62d6c772013-02-27 08:32:07 -0800275 if (LIKELY(!soa.Self()->IsExceptionPending())) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700276 if (shorty[0] == 'V' || (shorty[0] == 'L' && result == nullptr)) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800277 // Do nothing.
278 return zero;
279 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700280 StackHandleScope<1> hs(soa.Self());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700281 auto h_interface_method(hs.NewHandle(soa.Decode<mirror::Method*>(interface_method_jobj)));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700282 // This can cause thread suspension.
Vladimir Marko05792b92015-08-03 11:56:49 +0100283 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
284 mirror::Class* result_type =
285 h_interface_method->GetArtMethod()->GetReturnType(true /* resolve */, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
Ian Rogers62d6c772013-02-27 08:32:07 -0800287 JValue result_unboxed;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000288 if (!UnboxPrimitiveForResult(result_ref, result_type, &result_unboxed)) {
Ian Rogers530f71c2013-02-22 23:29:00 -0800289 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800290 return zero;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800291 }
292 return result_unboxed;
293 }
294 } else {
295 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
296 // a UndeclaredThrowableException.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000297 mirror::Throwable* exception = soa.Self()->GetException();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800298 if (exception->IsCheckedException()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700300 mirror::Class* proxy_class = rcvr->GetClass();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700301 mirror::Method* interface_method = soa.Decode<mirror::Method*>(interface_method_jobj);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 ArtMethod* proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(
303 interface_method->GetArtMethod(), sizeof(void*));
304 auto* virtual_methods = proxy_class->GetVirtualMethodsPtr();
305 size_t num_virtuals = proxy_class->NumVirtualMethods();
Vladimir Marko14632852015-08-17 12:07:23 +0100306 size_t method_size = ArtMethod::Size(sizeof(void*));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 int throws_index = (reinterpret_cast<uintptr_t>(proxy_method) -
308 reinterpret_cast<uintptr_t>(virtual_methods)) / method_size;
309 CHECK_LT(throws_index, static_cast<int>(num_virtuals));
Brian Carlstrom34375312014-09-10 23:10:47 -0700310 mirror::ObjectArray<mirror::Class>* declared_exceptions =
311 proxy_class->GetThrows()->Get(throws_index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 mirror::Class* exception_class = exception->GetClass();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800313 bool declares_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 for (int32_t i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 mirror::Class* declared_exception = declared_exceptions->Get(i);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800316 declares_exception = declared_exception->IsAssignableFrom(exception_class);
317 }
318 if (!declares_exception) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000319 soa.Self()->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;",
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700320 nullptr);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800321 }
322 }
323 return zero;
324 }
325}
Ian Rogers832336b2014-10-08 15:35:22 -0700326
327bool FillArrayData(mirror::Object* obj, const Instruction::ArrayDataPayload* payload) {
328 DCHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
329 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000330 ThrowNullPointerException("null array in FILL_ARRAY_DATA");
Ian Rogers832336b2014-10-08 15:35:22 -0700331 return false;
332 }
333 mirror::Array* array = obj->AsArray();
334 DCHECK(!array->IsObjectArray());
335 if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) {
336 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000337 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers832336b2014-10-08 15:35:22 -0700338 "failed FILL_ARRAY_DATA; length=%d, index=%d",
339 array->GetLength(), payload->element_count);
340 return false;
341 }
342 // Copy data from dex file to memory assuming both are little endian.
343 uint32_t size_in_bytes = payload->element_count * payload->element_width;
344 memcpy(array->GetRawData(payload->element_width, 0), payload->data, size_in_bytes);
345 return true;
346}
347
Shih-wei Liao2d831012011-09-28 22:06:53 -0700348} // namespace art