blob: 6301362e09a49795bcb5d9932b58175818f9c012 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "dex_file-inl.h"
Nicolas Geoffray1920c102015-09-29 18:00:03 +000025#include "entrypoints/entrypoint_utils-inl.h"
26#include "entrypoints/quick/callee_save_frame.h"
27#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/class-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070030#include "mirror/method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/object-inl.h"
32#include "mirror/object_array-inl.h"
Nicolas Geoffray1920c102015-09-29 18:00:03 +000033#include "nth_caller_visitor.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010034#include "oat_quick_method_header.h"
Ian Rogersaf6e67a2013-01-16 08:38:37 -080035#include "reflection.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070036#include "scoped_thread_state_change-inl.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
Mathieu Chartierbe08cf52016-09-13 13:41:24 -070041void CheckReferenceResult(Handle<mirror::Object> o, Thread* self) {
Andreas Gampefa4333d2017-02-14 11:10:34 -080042 if (o == nullptr) {
Ian Rogerse5877a12014-07-16 12:06:35 -070043 return;
44 }
Ian Rogerse5877a12014-07-16 12:06:35 -070045 // Make sure that the result is an instance of the type this method was expected to return.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -070046 ArtMethod* method = self->GetCurrentMethod(nullptr);
Vladimir Marko942fd312017-01-16 20:52:19 +000047 mirror::Class* return_type = method->GetReturnType(true /* resolve */);
Ian Rogerse5877a12014-07-16 12:06:35 -070048
49 if (!o->InstanceOf(return_type)) {
Ian Rogersc0542af2014-09-03 16:16:56 -070050 Runtime::Current()->GetJavaVM()->JniAbortF(nullptr,
51 "attempt to return an instance of %s from %s",
David Sehr709b0702016-10-13 09:12:37 -070052 o->PrettyTypeOf().c_str(),
53 method->PrettyMethod().c_str());
Ian Rogerse5877a12014-07-16 12:06:35 -070054 }
55}
56
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070057JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Ian Rogersaf6e67a2013-01-16 08:38:37 -080058 jobject rcvr_jobj, jobject interface_method_jobj,
59 std::vector<jvalue>& args) {
60 DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
61
62 // Build argument array possibly triggering GC.
63 soa.Self()->AssertThreadSuspensionIsAllowable();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070064 jobjectArray args_jobj = nullptr;
Ian Rogersaf6e67a2013-01-16 08:38:37 -080065 const JValue zero;
Jeff Haof00571c2014-05-29 17:29:47 -070066 int32_t target_sdk_version = Runtime::Current()->GetTargetSdkVersion();
67 // Do not create empty arrays unless needed to maintain Dalvik bug compatibility.
68 if (args.size() > 0 || (target_sdk_version > 0 && target_sdk_version <= 21)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, nullptr);
70 if (args_jobj == nullptr) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -080071 CHECK(soa.Self()->IsExceptionPending());
72 return zero;
73 }
74 for (size_t i = 0; i < args.size(); ++i) {
75 if (shorty[i + 1] == 'L') {
76 jobject val = args.at(i).l;
77 soa.Env()->SetObjectArrayElement(args_jobj, i, val);
78 } else {
79 JValue jv;
80 jv.SetJ(args.at(i).j);
Mathieu Chartier1cc62e42016-10-03 18:01:28 -070081 mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv).Ptr();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 if (val == nullptr) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -080083 CHECK(soa.Self()->IsExceptionPending());
84 return zero;
85 }
Mathieu Chartier0795f232016-09-27 18:43:30 -070086 soa.Decode<mirror::ObjectArray<mirror::Object>>(args_jobj)->Set<false>(i, val);
Ian Rogersaf6e67a2013-01-16 08:38:37 -080087 }
88 }
89 }
90
Mathieu Chartierfc58af42015-04-16 18:00:39 -070091 // Call Proxy.invoke(Proxy proxy, Method method, Object[] args).
Ian Rogersaf6e67a2013-01-16 08:38:37 -080092 jvalue invocation_args[3];
93 invocation_args[0].l = rcvr_jobj;
94 invocation_args[1].l = interface_method_jobj;
95 invocation_args[2].l = args_jobj;
96 jobject result =
Brian Carlstromea46f952013-07-30 01:26:50 -070097 soa.Env()->CallStaticObjectMethodA(WellKnownClasses::java_lang_reflect_Proxy,
98 WellKnownClasses::java_lang_reflect_Proxy_invoke,
99 invocation_args);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800100
101 // Unbox result and handle error conditions.
Ian Rogers62d6c772013-02-27 08:32:07 -0800102 if (LIKELY(!soa.Self()->IsExceptionPending())) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700103 if (shorty[0] == 'V' || (shorty[0] == 'L' && result == nullptr)) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800104 // Do nothing.
105 return zero;
106 } else {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700107 ArtMethod* interface_method =
Mathieu Chartier0795f232016-09-27 18:43:30 -0700108 soa.Decode<mirror::Method>(interface_method_jobj)->GetArtMethod();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700109 // This can cause thread suspension.
Vladimir Marko942fd312017-01-16 20:52:19 +0000110 mirror::Class* result_type = interface_method->GetReturnType(true /* resolve */);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700111 ObjPtr<mirror::Object> result_ref = soa.Decode<mirror::Object>(result);
Ian Rogers62d6c772013-02-27 08:32:07 -0800112 JValue result_unboxed;
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700113 if (!UnboxPrimitiveForResult(result_ref.Ptr(), result_type, &result_unboxed)) {
Ian Rogers530f71c2013-02-22 23:29:00 -0800114 DCHECK(soa.Self()->IsExceptionPending());
Ian Rogers62d6c772013-02-27 08:32:07 -0800115 return zero;
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800116 }
117 return result_unboxed;
118 }
119 } else {
120 // In the case of checked exceptions that aren't declared, the exception must be wrapped by
121 // a UndeclaredThrowableException.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000122 mirror::Throwable* exception = soa.Self()->GetException();
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800123 if (exception->IsCheckedException()) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800124 bool declares_exception = false;
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700125 {
126 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700127 ObjPtr<mirror::Object> rcvr = soa.Decode<mirror::Object>(rcvr_jobj);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700128 mirror::Class* proxy_class = rcvr->GetClass();
Mathieu Chartier0795f232016-09-27 18:43:30 -0700129 ObjPtr<mirror::Method> interface_method = soa.Decode<mirror::Method>(interface_method_jobj);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700130 ArtMethod* proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(
131 interface_method->GetArtMethod(), kRuntimePointerSize);
132 auto virtual_methods = proxy_class->GetVirtualMethodsSlice(kRuntimePointerSize);
133 size_t num_virtuals = proxy_class->NumVirtualMethods();
134 size_t method_size = ArtMethod::Size(kRuntimePointerSize);
135 // Rely on the fact that the methods are contiguous to determine the index of the method in
136 // the slice.
137 int throws_index = (reinterpret_cast<uintptr_t>(proxy_method) -
138 reinterpret_cast<uintptr_t>(&virtual_methods.At(0))) / method_size;
139 CHECK_LT(throws_index, static_cast<int>(num_virtuals));
140 mirror::ObjectArray<mirror::Class>* declared_exceptions =
141 proxy_class->GetThrows()->Get(throws_index);
142 mirror::Class* exception_class = exception->GetClass();
143 for (int32_t i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
144 mirror::Class* declared_exception = declared_exceptions->Get(i);
145 declares_exception = declared_exception->IsAssignableFrom(exception_class);
146 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800147 }
148 if (!declares_exception) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000149 soa.Self()->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;",
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700150 nullptr);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800151 }
152 }
153 return zero;
154 }
155}
Ian Rogers832336b2014-10-08 15:35:22 -0700156
Mathieu Chartieref41db72016-10-25 15:08:01 -0700157bool FillArrayData(ObjPtr<mirror::Object> obj, const Instruction::ArrayDataPayload* payload) {
Ian Rogers832336b2014-10-08 15:35:22 -0700158 DCHECK_EQ(payload->ident, static_cast<uint16_t>(Instruction::kArrayDataSignature));
159 if (UNLIKELY(obj == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000160 ThrowNullPointerException("null array in FILL_ARRAY_DATA");
Ian Rogers832336b2014-10-08 15:35:22 -0700161 return false;
162 }
163 mirror::Array* array = obj->AsArray();
164 DCHECK(!array->IsObjectArray());
165 if (UNLIKELY(static_cast<int32_t>(payload->element_count) > array->GetLength())) {
166 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000167 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers832336b2014-10-08 15:35:22 -0700168 "failed FILL_ARRAY_DATA; length=%d, index=%d",
169 array->GetLength(), payload->element_count);
170 return false;
171 }
172 // Copy data from dex file to memory assuming both are little endian.
173 uint32_t size_in_bytes = payload->element_count * payload->element_width;
174 memcpy(array->GetRawData(payload->element_width, 0), payload->data, size_in_bytes);
175 return true;
176}
177
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000178static inline std::pair<ArtMethod*, uintptr_t> DoGetCalleeSaveMethodOuterCallerAndPc(
179 ArtMethod** sp, Runtime::CalleeSaveType type) REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000180 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(type));
181
182 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA, type);
183 auto** caller_sp = reinterpret_cast<ArtMethod**>(
184 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100185 const size_t callee_return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, type);
186 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(
187 (reinterpret_cast<uint8_t*>(sp) + callee_return_pc_offset));
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000188 ArtMethod* outer_method = *caller_sp;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000189 return std::make_pair(outer_method, caller_pc);
190}
191
192static inline ArtMethod* DoGetCalleeSaveMethodCaller(ArtMethod* outer_method,
193 uintptr_t caller_pc,
194 bool do_caller_check)
195 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000196 ArtMethod* caller = outer_method;
Nicolas Geoffray63e47f42015-11-05 13:26:17 +0000197 if (LIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()))) {
198 if (outer_method != nullptr) {
199 const OatQuickMethodHeader* current_code = outer_method->GetOatQuickMethodHeader(caller_pc);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100200 DCHECK(current_code != nullptr);
201 DCHECK(current_code->IsOptimized());
202 uintptr_t native_pc_offset = current_code->NativeQuickPcOffset(caller_pc);
203 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
204 CodeInfoEncoding encoding = code_info.ExtractEncoding();
205 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset, encoding);
206 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800207 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100208 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
209 caller = GetResolvedMethod(outer_method,
210 inline_info,
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800211 encoding.inline_info.encoding,
212 inline_info.GetDepth(encoding.inline_info.encoding) - 1);
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000213 }
214 }
Nicolas Geoffray63e47f42015-11-05 13:26:17 +0000215 if (kIsDebugBuild && do_caller_check) {
216 // Note that do_caller_check is optional, as this method can be called by
217 // stubs, and tests without a proper call stack.
218 NthCallerVisitor visitor(Thread::Current(), 1, true);
219 visitor.WalkStack();
220 CHECK_EQ(caller, visitor.caller);
221 }
222 } else {
223 // We're instrumenting, just use the StackVisitor which knows how to
224 // handle instrumented frames.
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000225 NthCallerVisitor visitor(Thread::Current(), 1, true);
226 visitor.WalkStack();
Nicolas Geoffray63e47f42015-11-05 13:26:17 +0000227 caller = visitor.caller;
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000228 }
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000229 return caller;
230}
231
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000232ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp,
233 Runtime::CalleeSaveType type,
234 bool do_caller_check)
235 REQUIRES_SHARED(Locks::mutator_lock_) {
236 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
237 auto outer_caller_and_pc = DoGetCalleeSaveMethodOuterCallerAndPc(sp, type);
238 ArtMethod* outer_method = outer_caller_and_pc.first;
239 uintptr_t caller_pc = outer_caller_and_pc.second;
240 ArtMethod* caller = DoGetCalleeSaveMethodCaller(outer_method, caller_pc, do_caller_check);
241 return caller;
242}
243
244CallerAndOuterMethod GetCalleeSaveMethodCallerAndOuterMethod(Thread* self,
245 Runtime::CalleeSaveType type) {
246 CallerAndOuterMethod result;
247 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
248 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
249 auto outer_caller_and_pc = DoGetCalleeSaveMethodOuterCallerAndPc(sp, type);
250 result.outer_method = outer_caller_and_pc.first;
251 uintptr_t caller_pc = outer_caller_and_pc.second;
252 result.caller =
253 DoGetCalleeSaveMethodCaller(result.outer_method, caller_pc, /* do_caller_check */ true);
254 return result;
255}
256
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000257ArtMethod* GetCalleeSaveOuterMethod(Thread* self, Runtime::CalleeSaveType type) {
258 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
259 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
260 return DoGetCalleeSaveMethodOuterCallerAndPc(sp, type).first;
261}
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000262
Shih-wei Liao2d831012011-09-28 22:06:53 -0700263} // namespace art