blob: 87e29ae3c3bf6eff94f99338e3488b8d1c116d09 [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"
Nicolas Geoffray1920c102015-09-29 18:00:03 +000024#include "entrypoints/entrypoint_utils-inl.h"
25#include "entrypoints/quick/callee_save_frame.h"
26#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "mirror/class-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070029#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/object-inl.h"
31#include "mirror/object_array-inl.h"
Nicolas Geoffray1920c102015-09-29 18:00:03 +000032#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010033#include "oat_quick_method_header.h"
Ian Rogersaf6e67a2013-01-16 08:38:37 -080034#include "reflection.h"
35#include "scoped_thread_state_change.h"
TDYa1275bb86012012-04-11 05:57:28 -070036#include "ScopedLocalRef.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070037#include "well_known_classes.h"
TDYa1275bb86012012-04-11 05:57:28 -070038
jeffhao41005dd2012-05-09 17:58:52 -070039namespace art {
40
Brian Carlstrom34375312014-09-10 23:10:47 -070041static inline mirror::Class* CheckFilledNewArrayAlloc(uint32_t type_idx,
Brian Carlstrom34375312014-09-10 23:10:47 -070042 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -070043 ArtMethod* referrer,
Brian Carlstrom34375312014-09-10 23:10:47 -070044 Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080045 bool access_check)
Mathieu Chartier90443472015-07-16 20:32:27 -070046 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070047 if (UNLIKELY(component_count < 0)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080048 ThrowNegativeArraySizeException(component_count);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080049 return nullptr; // Failure
Elliott Hughes6c8867d2011-10-03 16:34:05 -070050 }
Vladimir Marko05792b92015-08-03 11:56:49 +010051 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
52 size_t pointer_size = class_linker->GetImagePointerSize();
53 mirror::Class* klass = referrer->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070054 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +010055 klass = class_linker->ResolveType(type_idx, referrer);
Mathieu Chartier2cebb242015-04-21 16:50:40 -070056 if (klass == nullptr) { // Error
Ian Rogers50b35e22012-10-04 10:09:15 -070057 DCHECK(self->IsExceptionPending());
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080058 return nullptr; // Failure
Ian Rogers19846512012-02-24 11:42:47 -080059 }
Ian Rogersea2a11d2011-10-11 16:48:51 -070060 }
Ian Rogers57b86d42012-03-27 16:05:41 -070061 if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
62 if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
Ian Rogers62d6c772013-02-27 08:32:07 -080063 ThrowRuntimeException("Bad filled array request for type %s",
64 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -080065 } else {
Brian Carlstrom34375312014-09-10 23:10:47 -070066 self->ThrowNewExceptionF(
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000067 "Ljava/lang/InternalError;",
Brian Carlstrom34375312014-09-10 23:10:47 -070068 "Found type %s; filled-new-array not implemented for anything but 'int'",
69 PrettyDescriptor(klass).c_str());
Ian Rogers573db4a2011-12-13 15:30:50 -080070 }
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080071 return nullptr; // Failure
Ian Rogers57b86d42012-03-27 16:05:41 -070072 }
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070073 if (access_check) {
74 mirror::Class* referrer_klass = referrer->GetDeclaringClass();
75 if (UNLIKELY(!referrer_klass->CanAccess(klass))) {
76 ThrowIllegalAccessErrorClass(referrer_klass, klass);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080077 return nullptr; // Failure
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070078 }
79 }
80 DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080081 return klass;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070082}
83
84// Helper function to allocate array for FILLED_NEW_ARRAY.
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080085mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -070086 ArtMethod* referrer, Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080087 bool access_check,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080088 gc::AllocatorType /* allocator_type */) {
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080089 mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, component_count, referrer, self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080090 access_check);
91 if (UNLIKELY(klass == nullptr)) {
92 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070093 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080094 // Always go slow path for now, filled new array is not common.
95 gc::Heap* heap = Runtime::Current()->GetHeap();
96 // Use the current allocator type in case CheckFilledNewArrayAlloc caused us to suspend and then
97 // the heap switched the allocator type while we were suspended.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -070098 return mirror::Array::Alloc<false>(self, klass, component_count,
99 klass->GetComponentSizeShift(),
Ian Rogers6fac4472014-02-25 17:01:10 -0800100 heap->GetCurrentAllocator());
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700101}
102
103// Helper function to allocate array for FILLED_NEW_ARRAY.
Brian Carlstrom34375312014-09-10 23:10:47 -0700104mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
Brian Carlstrom34375312014-09-10 23:10:47 -0700105 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700106 ArtMethod* referrer,
Brian Carlstrom34375312014-09-10 23:10:47 -0700107 Thread* self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800108 bool access_check,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800109 gc::AllocatorType /* allocator_type */) {
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800110 mirror::Class* klass = CheckFilledNewArrayAlloc(type_idx, component_count, referrer, self,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800111 access_check);
112 if (UNLIKELY(klass == nullptr)) {
113 return nullptr;
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700114 }
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800115 gc::Heap* heap = Runtime::Current()->GetHeap();
116 // Use the current allocator type in case CheckFilledNewArrayAlloc caused us to suspend and then
117 // the heap switched the allocator type while we were suspended.
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700118 return mirror::Array::Alloc<true>(self, klass, component_count,
119 klass->GetComponentSizeShift(),
Ian Rogers6fac4472014-02-25 17:01:10 -0800120 heap->GetCurrentAllocator());
Ian Rogers57b86d42012-03-27 16:05:41 -0700121}
122
jeffhaod7521322012-11-21 15:38:24 -0800123void ThrowStackOverflowError(Thread* self) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700124 if (self->IsHandlingStackOverflow()) {
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700125 LOG(ERROR) << "Recursive stack overflow.";
126 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700127 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800128
jeffhaod7521322012-11-21 15:38:24 -0800129 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
130 JNIEnvExt* env = self->GetJniEnv();
131 std::string msg("stack size ");
132 msg += PrettySize(self->GetStackSize());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700133
134 // Avoid running Java code for exception initialization.
135 // TODO: Checks to make this a bit less brittle.
136
137 std::string error_msg;
138
139 // Allocate an uninitialized object.
140 ScopedLocalRef<jobject> exc(env,
141 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
142 if (exc.get() != nullptr) {
143 // "Initialize".
144 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
145 // Only Throwable has "custom" fields:
146 // String detailMessage.
147 // Throwable cause (= this).
148 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
149 // Object stackState;
150 // StackTraceElement[] stackTrace;
151 // Only Throwable has a non-empty constructor:
152 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
153 // fillInStackTrace();
154
155 // detailMessage.
156 // TODO: Use String::FromModifiedUTF...?
157 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
158 if (s.get() != nullptr) {
Brian Carlstrom34375312014-09-10 23:10:47 -0700159 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700160
161 // cause.
Brian Carlstrom34375312014-09-10 23:10:47 -0700162 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700163
164 // suppressedExceptions.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700165 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
Brian Carlstrom34375312014-09-10 23:10:47 -0700166 WellKnownClasses::java_util_Collections,
167 WellKnownClasses::java_util_Collections_EMPTY_LIST));
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700168 CHECK(emptylist.get() != nullptr);
Brian Carlstrom34375312014-09-10 23:10:47 -0700169 env->SetObjectField(exc.get(),
170 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
171 emptylist.get());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700172
173 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
174 // nativeFillInStackTrace.
175 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
176 {
177 ScopedObjectAccessUnchecked soa(env);
178 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
179 }
180 if (stack_state_val.get() != nullptr) {
Brian Carlstrom34375312014-09-10 23:10:47 -0700181 env->SetObjectField(exc.get(),
182 WellKnownClasses::java_lang_Throwable_stackState,
183 stack_state_val.get());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700184
185 // stackTrace.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700186 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
Brian Carlstrom34375312014-09-10 23:10:47 -0700187 WellKnownClasses::libcore_util_EmptyArray,
188 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
189 env->SetObjectField(exc.get(),
190 WellKnownClasses::java_lang_Throwable_stackTrace,
191 stack_trace_elem.get());
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700192 } else {
193 error_msg = "Could not create stack trace.";
194 }
Mathieu Chartier50c138f2015-01-07 16:00:03 -0800195 // Throw the exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000196 self->SetException(reinterpret_cast<mirror::Throwable*>(self->DecodeJObject(exc.get())));
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700197 } else {
198 // Could not allocate a string object.
199 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
200 }
201 } else {
202 error_msg = "Could not allocate StackOverflowError object.";
203 }
204
205 if (!error_msg.empty()) {
Andreas Gampe1de0f5c2015-02-19 10:54:31 -0800206 LOG(WARNING) << error_msg;
jeffhaod7521322012-11-21 15:38:24 -0800207 CHECK(self->IsExceptionPending());
208 }
Dave Allisonf9439142014-03-27 15:10:22 -0700209
210 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
Dave Allisonb090a182014-08-14 17:02:48 +0000211 self->ResetDefaultStackEnd(); // Return to default stack size.
Dave Allison648d7112014-07-25 16:15:27 -0700212
213 // And restore protection if implicit checks are on.
214 if (!explicit_overflow_check) {
215 self->ProtectStack();
216 }
jeffhaod7521322012-11-21 15:38:24 -0800217}
218
Ian Rogerse5877a12014-07-16 12:06:35 -0700219void CheckReferenceResult(mirror::Object* o, Thread* self) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700220 if (o == nullptr) {
Ian Rogerse5877a12014-07-16 12:06:35 -0700221 return;
222 }
Ian Rogerse5877a12014-07-16 12:06:35 -0700223 // Make sure that the result is an instance of the type this method was expected to return.
Vladimir Marko05792b92015-08-03 11:56:49 +0100224 mirror::Class* return_type = self->GetCurrentMethod(nullptr)->GetReturnType(true /* resolve */,
225 sizeof(void*));
Ian Rogerse5877a12014-07-16 12:06:35 -0700226
227 if (!o->InstanceOf(return_type)) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700228 Runtime::Current()->GetJavaVM()->JniAbortF(nullptr,
229 "attempt to return an instance of %s from %s",
Ian Rogers68d8b422014-07-17 11:09:10 -0700230 PrettyTypeOf(o).c_str(),
Ian Rogersded66a02014-10-28 18:12:55 -0700231 PrettyMethod(self->GetCurrentMethod(nullptr)).c_str());
Ian Rogerse5877a12014-07-16 12:06:35 -0700232 }
233}
234
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700235JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800236 jobject rcvr_jobj, jobject interface_method_jobj,
237 std::vector<jvalue>& args) {
238 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
239
240 // Build argument array possibly triggering GC.
241 soa.Self()->AssertThreadSuspensionIsAllowable();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700242 jobjectArray args_jobj = nullptr;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800243 const JValue zero;
Jeff Haof00571c2014-05-29 17:29:47 -0700244 int32_t target_sdk_version = Runtime::Current()->GetTargetSdkVersion();
245 // Do not create empty arrays unless needed to maintain Dalvik bug compatibility.
246 if (args.size() > 0 || (target_sdk_version > 0 && target_sdk_version <= 21)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700247 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, nullptr);
248 if (args_jobj == nullptr) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800249 CHECK(soa.Self()->IsExceptionPending());
250 return zero;
251 }
252 for (size_t i = 0; i < args.size(); ++i) {
253 if (shorty[i + 1] == 'L') {
254 jobject val = args.at(i).l;
255 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
256 } else {
257 JValue jv;
258 jv.SetJ(args.at(i).j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800259 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700260 if (val == nullptr) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800261 CHECK(soa.Self()->IsExceptionPending());
262 return zero;
263 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100264 soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set<false>(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800265 }
266 }
267 }
268
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700269 // Call Proxy.invoke(Proxy proxy, Method method, Object[] args).
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800270 jvalue invocation_args[3];
271 invocation_args[0].l = rcvr_jobj;
272 invocation_args[1].l = interface_method_jobj;
273 invocation_args[2].l = args_jobj;
274 jobject result =
Brian Carlstromea46f952013-07-30 01:26:50 -0700275 soa.Env()->CallStaticObjectMethodA(WellKnownClasses::java_lang_reflect_Proxy,
276 WellKnownClasses::java_lang_reflect_Proxy_invoke,
277 invocation_args);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800278
279 // Unbox result and handle error conditions.
Ian Rogers62d6c772013-02-27 08:32:07 -0800280 if (LIKELY(!soa.Self()->IsExceptionPending())) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700281 if (shorty[0] == 'V' || (shorty[0] == 'L' && result == nullptr)) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800282 // Do nothing.
283 return zero;
284 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700285 StackHandleScope<1> hs(soa.Self());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700286 auto h_interface_method(hs.NewHandle(soa.Decode<mirror::Method*>(interface_method_jobj)));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700287 // This can cause thread suspension.
Vladimir Marko05792b92015-08-03 11:56:49 +0100288 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
289 mirror::Class* result_type =
290 h_interface_method->GetArtMethod()->GetReturnType(true /* resolve */, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 JValue result_unboxed;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000293 if (!UnboxPrimitiveForResult(result_ref, result_type, &result_unboxed)) {
Ian Rogers530f71c2013-02-22 23:29:00 -0800294 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 return zero;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800296 }
297 return result_unboxed;
298 }
299 } else {
300 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
301 // a UndeclaredThrowableException.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000302 mirror::Throwable* exception = soa.Self()->GetException();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800303 if (exception->IsCheckedException()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304 mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700305 mirror::Class* proxy_class = rcvr->GetClass();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700306 mirror::Method* interface_method = soa.Decode<mirror::Method*>(interface_method_jobj);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 ArtMethod* proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(
308 interface_method->GetArtMethod(), sizeof(void*));
309 auto* virtual_methods = proxy_class->GetVirtualMethodsPtr();
310 size_t num_virtuals = proxy_class->NumVirtualMethods();
Vladimir Marko14632852015-08-17 12:07:23 +0100311 size_t method_size = ArtMethod::Size(sizeof(void*));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 int throws_index = (reinterpret_cast<uintptr_t>(proxy_method) -
313 reinterpret_cast<uintptr_t>(virtual_methods)) / method_size;
314 CHECK_LT(throws_index, static_cast<int>(num_virtuals));
Brian Carlstrom34375312014-09-10 23:10:47 -0700315 mirror::ObjectArray<mirror::Class>* declared_exceptions =
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000316 proxy_class->GetThrows()->Get(throws_index);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317 mirror::Class* exception_class = exception->GetClass();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800318 bool declares_exception = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 for (int32_t i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 mirror::Class* declared_exception = declared_exceptions->Get(i);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800321 declares_exception = declared_exception->IsAssignableFrom(exception_class);
322 }
323 if (!declares_exception) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000324 soa.Self()->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;",
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700325 nullptr);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800326 }
327 }
328 return zero;
329 }
330}
Ian Rogers832336b2014-10-08 15:35:22 -0700331
332bool FillArrayData(mirror::Object* obj, const Instruction::ArrayDataPayload* payload) {
333 DCHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
334 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000335 ThrowNullPointerException("null array in FILL_ARRAY_DATA");
Ian Rogers832336b2014-10-08 15:35:22 -0700336 return false;
337 }
338 mirror::Array* array = obj->AsArray();
339 DCHECK(!array->IsObjectArray());
340 if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) {
341 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000342 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers832336b2014-10-08 15:35:22 -0700343 "failed FILL_ARRAY_DATA; length=%d, index=%d",
344 array->GetLength(), payload->element_count);
345 return false;
346 }
347 // Copy data from dex file to memory assuming both are little endian.
348 uint32_t size_in_bytes = payload->element_count * payload->element_width;
349 memcpy(array->GetRawData(payload->element_width, 0), payload->data, size_in_bytes);
350 return true;
351}
352
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000353ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp,
354 Runtime::CalleeSaveType type,
355 bool do_caller_check)
356 SHARED_REQUIRES(Locks::mutator_lock_) {
357 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
358
359 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
360 auto** caller_sp = reinterpret_cast<ArtMethod**>(
361 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100362 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type);
363 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
364 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000365 ArtMethod* outer_method = *caller_sp;
366 ArtMethod* caller = outer_method;
Nicolas Geoffray63e47f42015-11-05 13:26:17 +0000367 if (LIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
368 if (outer_method != nullptr) {
369 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
370 if (current_code->IsOptimized()) {
371 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
372 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
373 StackMapEncoding encoding = code_info.ExtractEncoding();
374 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
375 DCHECK(stack_map.IsValid());
376 if (stack_map.HasInlineInfo(encoding)) {
377 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
378 caller = GetResolvedMethod(outer_method, inline_info, inline_info.GetDepth() - 1);
379 }
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000380 }
381 }
Nicolas Geoffray63e47f42015-11-05 13:26:17 +0000382 if (kIsDebugBuild && do_caller_check) {
383 // Note that do_caller_check is optional, as this method can be called by
384 // stubs, and tests without a proper call stack.
385 NthCallerVisitor visitor(Thread::Current(), 1, true);
386 visitor.WalkStack();
387 CHECK_EQ(caller, visitor.caller);
388 }
389 } else {
390 // We're instrumenting, just use the StackVisitor which knows how to
391 // handle instrumented frames.
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000392 NthCallerVisitor visitor(Thread::Current(), 1, true);
393 visitor.WalkStack();
Nicolas Geoffray63e47f42015-11-05 13:26:17 +0000394 caller = visitor.caller;
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000395 }
396
397 return caller;
398}
399
Shih-wei Liao2d831012011-09-28 22:06:53 -0700400} // namespace art