blob: 19498f386ca91e240fb05f0e0595c73a2e54e316 [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"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070022#include "base/mutex.h"
David Brazdil2bb2fbd2018-11-13 18:24:26 +000023#include "base/sdk_version.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "class_linker-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file-inl.h"
Nicolas Geoffray1920c102015-09-29 18:00:03 +000026#include "entrypoints/entrypoint_utils-inl.h"
27#include "entrypoints/quick/callee_save_frame.h"
28#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010030#include "jni/java_vm_ext.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/class-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070032#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/object-inl.h"
34#include "mirror/object_array-inl.h"
Nicolas Geoffray1920c102015-09-29 18:00:03 +000035#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010036#include "oat_quick_method_header.h"
Ian Rogersaf6e67a2013-01-16 08:38:37 -080037#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070038#include "scoped_thread_state_change-inl.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070039#include "well_known_classes.h"
TDYa1275bb86012012-04-11 05:57:28 -070040
jeffhao41005dd2012-05-09 17:58:52 -070041namespace art {
42
Mathieu Chartierbe08cf52016-09-13 13:41:24 -070043void CheckReferenceResult(Handle<mirror::Object> o, Thread* self) {
Andreas Gampefa4333d2017-02-14 11:10:34 -080044 if (o == nullptr) {
Ian Rogerse5877a12014-07-16 12:06:35 -070045 return;
46 }
Ian Rogerse5877a12014-07-16 12:06:35 -070047 // Make sure that the result is an instance of the type this method was expected to return.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -070048 ArtMethod* method = self->GetCurrentMethod(nullptr);
Vladimir Markob45528c2017-07-27 14:14:28 +010049 ObjPtr<mirror::Class> return_type = method->ResolveReturnType();
Ian Rogerse5877a12014-07-16 12:06:35 -070050
51 if (!o->InstanceOf(return_type)) {
Ian Rogersc0542af2014-09-03 16:16:56 -070052 Runtime::Current()->GetJavaVM()->JniAbortF(nullptr,
53 "attempt to return an instance of %s from %s",
David Sehr709b0702016-10-13 09:12:37 -070054 o->PrettyTypeOf().c_str(),
55 method->PrettyMethod().c_str());
Ian Rogerse5877a12014-07-16 12:06:35 -070056 }
57}
58
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070059JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Ian Rogersaf6e67a2013-01-16 08:38:37 -080060 jobject rcvr_jobj, jobject interface_method_jobj,
61 std::vector<jvalue>& args) {
62 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
63
64 // Build argument array possibly triggering GC.
65 soa.Self()->AssertThreadSuspensionIsAllowable();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 jobjectArray args_jobj = nullptr;
Ian Rogersaf6e67a2013-01-16 08:38:37 -080067 const JValue zero;
David Brazdil2bb2fbd2018-11-13 18:24:26 +000068 uint32_t target_sdk_version = Runtime::Current()->GetTargetSdkVersion();
Jeff Haof00571c2014-05-29 17:29:47 -070069 // Do not create empty arrays unless needed to maintain Dalvik bug compatibility.
David Brazdil2bb2fbd2018-11-13 18:24:26 +000070 if (args.size() > 0 || IsSdkVersionSetAndAtMost(target_sdk_version, SdkVersion::kL)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070071 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, nullptr);
72 if (args_jobj == nullptr) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -080073 CHECK(soa.Self()->IsExceptionPending());
74 return zero;
75 }
76 for (size_t i = 0; i < args.size(); ++i) {
77 if (shorty[i + 1] == 'L') {
Vladimir Marko35d5b8a2018-07-03 09:18:32 +010078 jobject val = args[i].l;
Ian Rogersaf6e67a2013-01-16 08:38:37 -080079 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
80 } else {
81 JValue jv;
Vladimir Marko35d5b8a2018-07-03 09:18:32 +010082 jv.SetJ(args[i].j);
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070083 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv).Ptr();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 if (val == nullptr) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -080085 CHECK(soa.Self()->IsExceptionPending());
86 return zero;
87 }
Mathieu Chartier0795f232016-09-27 18:43:30 -070088 soa.Decode<mirror::ObjectArray<mirror::Object>>(args_jobj)->Set<false>(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -080089 }
90 }
91 }
92
Mathieu Chartierfc58af42015-04-16 18:00:39 -070093 // Call Proxy.invoke(Proxy proxy, Method method, Object[] args).
Ian Rogersaf6e67a2013-01-16 08:38:37 -080094 jvalue invocation_args[3];
95 invocation_args[0].l = rcvr_jobj;
96 invocation_args[1].l = interface_method_jobj;
97 invocation_args[2].l = args_jobj;
98 jobject result =
Brian Carlstromea46f952013-07-30 01:26:50 -070099 soa.Env()->CallStaticObjectMethodA(WellKnownClasses::java_lang_reflect_Proxy,
100 WellKnownClasses::java_lang_reflect_Proxy_invoke,
101 invocation_args);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800102
103 // Unbox result and handle error conditions.
Ian Rogers62d6c772013-02-27 08:32:07 -0800104 if (LIKELY(!soa.Self()->IsExceptionPending())) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700105 if (shorty[0] == 'V' || (shorty[0] == 'L' && result == nullptr)) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800106 // Do nothing.
107 return zero;
108 } else {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700109 ArtMethod* interface_method =
Mathieu Chartier0795f232016-09-27 18:43:30 -0700110 soa.Decode<mirror::Method>(interface_method_jobj)->GetArtMethod();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700111 // This can cause thread suspension.
Vladimir Markob45528c2017-07-27 14:14:28 +0100112 ObjPtr<mirror::Class> result_type = interface_method->ResolveReturnType();
Mathieu Chartier0795f232016-09-27 18:43:30 -0700113 ObjPtr<mirror::Object> result_ref = soa.Decode<mirror::Object>(result);
Ian Rogers62d6c772013-02-27 08:32:07 -0800114 JValue result_unboxed;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700115 if (!UnboxPrimitiveForResult(result_ref.Ptr(), result_type, &result_unboxed)) {
Ian Rogers530f71c2013-02-22 23:29:00 -0800116 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800117 return zero;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800118 }
119 return result_unboxed;
120 }
121 } else {
122 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
123 // a UndeclaredThrowableException.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000124 mirror::Throwable* exception = soa.Self()->GetException();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800125 if (exception->IsCheckedException()) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800126 bool declares_exception = false;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700127 {
128 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700129 ObjPtr<mirror::Object> rcvr = soa.Decode<mirror::Object>(rcvr_jobj);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700130 mirror::Class* proxy_class = rcvr->GetClass();
Mathieu Chartier0795f232016-09-27 18:43:30 -0700131 ObjPtr<mirror::Method> interface_method = soa.Decode<mirror::Method>(interface_method_jobj);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700132 ArtMethod* proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(
133 interface_method->GetArtMethod(), kRuntimePointerSize);
134 auto virtual_methods = proxy_class->GetVirtualMethodsSlice(kRuntimePointerSize);
135 size_t num_virtuals = proxy_class->NumVirtualMethods();
136 size_t method_size = ArtMethod::Size(kRuntimePointerSize);
137 // Rely on the fact that the methods are contiguous to determine the index of the method in
138 // the slice.
139 int throws_index = (reinterpret_cast<uintptr_t>(proxy_method) -
Vladimir Marko9ac77492017-06-14 18:07:03 +0100140 reinterpret_cast<uintptr_t>(&virtual_methods[0])) / method_size;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700141 CHECK_LT(throws_index, static_cast<int>(num_virtuals));
142 mirror::ObjectArray<mirror::Class>* declared_exceptions =
Narayan Kamath6b2dc312017-03-14 13:26:12 +0000143 proxy_class->GetProxyThrows()->Get(throws_index);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700144 mirror::Class* exception_class = exception->GetClass();
145 for (int32_t i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
146 mirror::Class* declared_exception = declared_exceptions->Get(i);
147 declares_exception = declared_exception->IsAssignableFrom(exception_class);
148 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800149 }
150 if (!declares_exception) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000151 soa.Self()->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;",
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700152 nullptr);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800153 }
154 }
155 return zero;
156 }
157}
Ian Rogers832336b2014-10-08 15:35:22 -0700158
Mathieu Chartieref41db72016-10-25 15:08:01 -0700159bool FillArrayData(ObjPtr<mirror::Object> obj, const Instruction::ArrayDataPayload* payload) {
Ian Rogers832336b2014-10-08 15:35:22 -0700160 DCHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
161 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000162 ThrowNullPointerException("null array in FILL_ARRAY_DATA");
Ian Rogers832336b2014-10-08 15:35:22 -0700163 return false;
164 }
165 mirror::Array* array = obj->AsArray();
166 DCHECK(!array->IsObjectArray());
167 if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) {
168 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000169 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers832336b2014-10-08 15:35:22 -0700170 "failed FILL_ARRAY_DATA; length=%d, index=%d",
171 array->GetLength(), payload->element_count);
172 return false;
173 }
174 // Copy data from dex file to memory assuming both are little endian.
175 uint32_t size_in_bytes = payload->element_count * payload->element_width;
176 memcpy(array->GetRawData(payload->element_width, 0), payload->data, size_in_bytes);
177 return true;
178}
179
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000180static inline std::pair<ArtMethod*, uintptr_t> DoGetCalleeSaveMethodOuterCallerAndPc(
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700181 ArtMethod** sp, CalleeSaveType type) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000182 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
183
Vladimir Markod3083dd2018-05-17 08:43:47 +0100184 const size_t callee_frame_size = RuntimeCalleeSaveFrame::GetFrameSize(type);
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000185 auto** caller_sp = reinterpret_cast<ArtMethod**>(
186 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Vladimir Markod3083dd2018-05-17 08:43:47 +0100187 const size_t callee_return_pc_offset = RuntimeCalleeSaveFrame::GetReturnPcOffset(type);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100188 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
189 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000190 ArtMethod* outer_method = *caller_sp;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000191 return std::make_pair(outer_method, caller_pc);
192}
193
194static inline ArtMethod* DoGetCalleeSaveMethodCaller(ArtMethod* outer_method,
195 uintptr_t caller_pc,
196 bool do_caller_check)
197 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000198 ArtMethod* caller = outer_method;
Nicolas Geoffray63e47f42015-11-05 13:26:17 +0000199 if (LIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
200 if (outer_method != nullptr) {
201 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100202 DCHECK(current_code != nullptr);
203 DCHECK(current_code->IsOptimized());
204 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
David Srbecky6ee06e92018-07-25 21:45:54 +0100205 CodeInfo code_info(current_code, CodeInfo::DecodeFlags::InlineInfoOnly);
David Srbecky052f8ca2018-04-26 15:42:54 +0100206 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100207 DCHECK(stack_map.IsValid());
David Srbecky93bd3612018-07-02 19:30:18 +0100208 BitTableRange<InlineInfo> inline_infos = code_info.GetInlineInfosOf(stack_map);
209 if (!inline_infos.empty()) {
David Srbecky8cd54542018-07-15 23:58:44 +0100210 caller = GetResolvedMethod(outer_method, code_info, inline_infos);
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000211 }
212 }
Nicolas Geoffray63e47f42015-11-05 13:26:17 +0000213 if (kIsDebugBuild && do_caller_check) {
214 // Note that do_caller_check is optional, as this method can be called by
215 // stubs, and tests without a proper call stack.
216 NthCallerVisitor visitor(Thread::Current(), 1, true);
217 visitor.WalkStack();
218 CHECK_EQ(caller, visitor.caller);
219 }
220 } else {
221 // We're instrumenting, just use the StackVisitor which knows how to
222 // handle instrumented frames.
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000223 NthCallerVisitor visitor(Thread::Current(), 1, true);
224 visitor.WalkStack();
Nicolas Geoffray63e47f42015-11-05 13:26:17 +0000225 caller = visitor.caller;
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000226 }
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000227 return caller;
228}
229
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700230ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp, CalleeSaveType type, bool do_caller_check)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000231 REQUIRES_SHARED(Locks::mutator_lock_) {
232 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
233 auto outer_caller_and_pc = DoGetCalleeSaveMethodOuterCallerAndPc(sp, type);
234 ArtMethod* outer_method = outer_caller_and_pc.first;
235 uintptr_t caller_pc = outer_caller_and_pc.second;
236 ArtMethod* caller = DoGetCalleeSaveMethodCaller(outer_method, caller_pc, do_caller_check);
237 return caller;
238}
239
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700240CallerAndOuterMethod GetCalleeSaveMethodCallerAndOuterMethod(Thread* self, CalleeSaveType type) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000241 CallerAndOuterMethod result;
242 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Marko2196c652017-11-30 16:16:07 +0000243 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrameKnownNotTagged();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 auto outer_caller_and_pc = DoGetCalleeSaveMethodOuterCallerAndPc(sp, type);
245 result.outer_method = outer_caller_and_pc.first;
246 uintptr_t caller_pc = outer_caller_and_pc.second;
247 result.caller =
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700248 DoGetCalleeSaveMethodCaller(result.outer_method, caller_pc, /* do_caller_check= */ true);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000249 return result;
250}
251
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700252ArtMethod* GetCalleeSaveOuterMethod(Thread* self, CalleeSaveType type) {
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000253 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Marko2196c652017-11-30 16:16:07 +0000254 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrameKnownNotTagged();
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000255 return DoGetCalleeSaveMethodOuterCallerAndPc(sp, type).first;
256}
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000257
Orion Hodsondbaa5c72018-05-10 08:22:46 +0100258ObjPtr<mirror::MethodHandle> ResolveMethodHandleFromCode(ArtMethod* referrer,
259 uint32_t method_handle_idx) {
260 Thread::PoisonObjectPointersIfDebug();
261 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
262 return class_linker->ResolveMethodHandle(Thread::Current(), method_handle_idx, referrer);
263}
264
Orion Hodson18259d72018-04-12 11:18:23 +0100265ObjPtr<mirror::MethodType> ResolveMethodTypeFromCode(ArtMethod* referrer,
Orion Hodson06d10a72018-05-14 08:53:38 +0100266 dex::ProtoIndex proto_idx) {
Orion Hodson18259d72018-04-12 11:18:23 +0100267 Thread::PoisonObjectPointersIfDebug();
268 ObjPtr<mirror::MethodType> method_type =
269 referrer->GetDexCache()->GetResolvedMethodType(proto_idx);
270 if (UNLIKELY(method_type == nullptr)) {
271 StackHandleScope<2> hs(Thread::Current());
272 Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache()));
273 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader()));
274 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
275 method_type = class_linker->ResolveMethodType(hs.Self(), proto_idx, dex_cache, class_loader);
276 }
277 return method_type;
278}
279
Shih-wei Liao2d831012011-09-28 22:06:53 -0700280} // namespace art