blob: a10d7afb0f3f817b862efb9f9926b527ef97e7e2 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
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
Brian Carlstromea46f952013-07-30 01:26:50 -070017#include "art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018
Ian Rogerse63db272014-07-15 15:36:11 -070019#include "arch/context.h"
Ian Rogers62f05122014-03-21 11:21:29 -070020#include "art_field-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "base/stringpiece.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070023#include "class_linker-inl.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070024#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersc449aa82013-07-29 14:35:46 -070026#include "dex_instruction.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070027#include "entrypoints/entrypoint_utils.h"
28#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "jit/jit.h"
32#include "jit/jit_code_cache.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010033#include "jit/profiling_info.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "jni_internal.h"
Ian Rogers1809a722013-08-09 22:05:32 -070035#include "mapping_table.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070036#include "mirror/abstract_method.h"
37#include "mirror/class-inl.h"
38#include "mirror/object_array-inl.h"
39#include "mirror/object-inl.h"
40#include "mirror/string.h"
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000041#include "oat_file-inl.h"
Ian Rogers62f05122014-03-21 11:21:29 -070042#include "scoped_thread_state_change.h"
Ian Rogers62f05122014-03-21 11:21:29 -070043#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044
45namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046
Ian Rogers0177e532014-02-11 16:30:46 -080047extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
48 const char*);
Ian Rogers936b37f2014-02-14 00:52:24 -080049extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
50 const char*);
Jeff Hao5d917302013-02-27 17:57:33 -080051
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070052ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
53 jobject jlr_method) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -070054 auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(jlr_method);
55 DCHECK(abstract_method != nullptr);
56 return abstract_method->GetArtMethod();
Ian Rogers62f05122014-03-21 11:21:29 -070057}
58
Ian Rogers6b14d552014-10-28 21:50:58 -070059mirror::String* ArtMethod::GetNameAsString(Thread* self) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 CHECK(!IsProxyMethod());
Ian Rogers6b14d552014-10-28 21:50:58 -070061 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
63 auto* dex_file = dex_cache->GetDexFile();
64 uint32_t dex_method_idx = GetDexMethodIndex();
65 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
Ian Rogers6b14d552014-10-28 21:50:58 -070066 return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
67 dex_cache);
68}
69
Ian Rogersef7d42f2014-01-06 12:55:46 -080070InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 // TODO: kSuper?
72 if (GetDeclaringClass()->IsInterface()) {
73 return kInterface;
74 } else if (IsStatic()) {
75 return kStatic;
76 } else if (IsDirect()) {
77 return kDirect;
78 } else {
79 return kVirtual;
80 }
81}
82
Brian Carlstromea46f952013-07-30 01:26:50 -070083size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -070084 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -070086 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 char ch = shorty[i];
88 if (ch == 'D' || ch == 'J') {
89 num_registers += 2;
90 } else {
91 num_registers += 1;
92 }
93 }
94 return num_registers;
95}
96
Ian Rogersf2247512014-12-02 16:17:08 -080097static bool HasSameNameAndSignature(ArtMethod* method1, ArtMethod* method2)
Mathieu Chartier90443472015-07-16 20:32:27 -070098 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersf2247512014-12-02 16:17:08 -080099 ScopedAssertNoThreadSuspension ants(Thread::Current(), "HasSameNameAndSignature");
100 const DexFile* dex_file = method1->GetDexFile();
101 const DexFile::MethodId& mid = dex_file->GetMethodId(method1->GetDexMethodIndex());
102 if (method1->GetDexCache() == method2->GetDexCache()) {
103 const DexFile::MethodId& mid2 = dex_file->GetMethodId(method2->GetDexMethodIndex());
104 return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
105 }
106 const DexFile* dex_file2 = method2->GetDexFile();
107 const DexFile::MethodId& mid2 = dex_file2->GetMethodId(method2->GetDexMethodIndex());
108 if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
109 return false; // Name mismatch.
110 }
111 return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
112}
113
Mathieu Chartiere401d142015-04-22 13:56:20 -0700114ArtMethod* ArtMethod::FindOverriddenMethod(size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 if (IsStatic()) {
Ian Rogersf2247512014-12-02 16:17:08 -0800116 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 mirror::Class* declaring_class = GetDeclaringClass();
119 mirror::Class* super_class = declaring_class->GetSuperClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 uint16_t method_index = GetMethodIndex();
Ian Rogersf2247512014-12-02 16:17:08 -0800121 ArtMethod* result = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 // Did this method override a super class method? If so load the result from the super class'
123 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700124 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700125 result = super_class->GetVTableEntry(method_index, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 } else {
127 // Method didn't override superclass method so search interfaces
128 if (IsProxyMethod()) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100129 result = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
130 GetDexMethodIndex(),
131 pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132 CHECK_EQ(result,
133 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
134 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 mirror::IfTable* iftable = GetDeclaringClass()->GetIfTable();
Ian Rogersf2247512014-12-02 16:17:08 -0800136 for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700137 mirror::Class* interface = iftable->GetInterface(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700139 ArtMethod* interface_method = interface->GetVirtualMethod(j, pointer_size);
140 if (HasSameNameAndSignature(
141 this, interface_method->GetInterfaceMethodIfProxy(sizeof(void*)))) {
Ian Rogersf2247512014-12-02 16:17:08 -0800142 result = interface_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 break;
144 }
145 }
146 }
147 }
148 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700149 DCHECK(result == nullptr || HasSameNameAndSignature(
150 GetInterfaceMethodIfProxy(sizeof(void*)), result->GetInterfaceMethodIfProxy(sizeof(void*))));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 return result;
152}
153
Ian Rogerse0a02da2014-12-02 14:10:53 -0800154uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
155 uint32_t name_and_signature_idx) {
156 const DexFile* dexfile = GetDexFile();
157 const uint32_t dex_method_idx = GetDexMethodIndex();
158 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
159 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
160 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
161 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
162 if (dexfile == &other_dexfile) {
163 return dex_method_idx;
164 }
165 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
166 const DexFile::StringId* other_descriptor =
167 other_dexfile.FindStringId(mid_declaring_class_descriptor);
168 if (other_descriptor != nullptr) {
169 const DexFile::TypeId* other_type_id =
170 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
171 if (other_type_id != nullptr) {
172 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
173 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
174 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
175 if (other_mid != nullptr) {
176 return other_dexfile.GetIndexForMethodId(*other_mid);
177 }
178 }
179 }
180 return DexFile::kDexNoIndex;
181}
182
Mathieu Chartiere401d142015-04-22 13:56:20 -0700183uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700184 uint32_t dex_pc, bool* has_no_move_exception) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700185 const DexFile::CodeItem* code_item = GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700186 // Set aside the exception while we resolve its type.
187 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700188 StackHandleScope<1> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000189 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Jeff Haoaa961912014-04-22 13:54:32 -0700190 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700191 // Default to handler not found.
192 uint32_t found_dex_pc = DexFile::kDexNoIndex;
193 // Iterate over the catch handlers associated with dex_pc.
Vladimir Marko05792b92015-08-03 11:56:49 +0100194 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
196 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
197 // Catch all case
198 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700199 found_dex_pc = it.GetHandlerAddress();
200 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 }
202 // Does this catch exception type apply?
Vladimir Marko05792b92015-08-03 11:56:49 +0100203 mirror::Class* iter_exception_type = GetClassFromTypeIndex(iter_type_idx,
204 true /* resolve */,
205 pointer_size);
Ian Rogers822266b2014-05-29 16:55:06 -0700206 if (UNLIKELY(iter_exception_type == nullptr)) {
207 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
208 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700209 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700210 self->ClearException();
211 // Delete any long jump context as this routine is called during a stack walk which will
212 // release its in use context at the end.
213 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700215 << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700216 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700217 found_dex_pc = it.GetHandlerAddress();
218 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 }
220 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700221 if (found_dex_pc != DexFile::kDexNoIndex) {
222 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700223 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700224 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
225 }
Jeff Haoaa961912014-04-22 13:54:32 -0700226 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700227 if (exception.Get() != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000228 self->SetException(exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700229 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700230 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231}
232
Brian Carlstromea46f952013-07-30 01:26:50 -0700233void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800234 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700235 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
236 ThrowStackOverflowError(self);
237 return;
238 }
239
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 if (kIsDebugBuild) {
241 self->AssertThreadSuspensionIsAllowable();
242 CHECK_EQ(kRunnable, self->GetState());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700243 CHECK_STREQ(GetInterfaceMethodIfProxy(sizeof(void*))->GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 }
245
246 // Push a transition back into managed code onto the linked list in thread.
247 ManagedStack fragment;
248 self->PushManagedStackFragment(&fragment);
249
Ian Rogers62d6c772013-02-27 08:32:07 -0800250 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700251 // Call the invoke stub, passing everything as arguments.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200252 // If the runtime is not yet started or it is required by the debugger, then perform the
253 // Invocation by the interpreter.
254 if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this))) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700255 if (IsStatic()) {
256 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result);
257 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258 mirror::Object* receiver =
259 reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
Ian Rogers5d27faf2014-05-02 17:17:18 -0700260 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 }
262 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), sizeof(void*));
264
265 constexpr bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800266 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800267 if (LIKELY(have_quick_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700268 if (kLogInvocationStartAndReturn) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 LOG(INFO) << StringPrintf(
270 "Invoking '%s' quick code=%p static=%d", PrettyMethod(this).c_str(),
271 GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0));
Jeff Hao790ad902013-05-22 15:02:08 -0700272 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700273
Elliott Hughes956af0f2014-12-11 14:34:28 -0800274 // Ensure that we won't be accidentally calling quick compiled code when -Xint.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800275 if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100276 CHECK(!runtime->UseJit());
277 const void* oat_quick_code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(this);
278 CHECK(oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode())
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700279 << "Don't call compiled code when -Xint " << PrettyMethod(this);
280 }
281
Elliott Hughes956af0f2014-12-11 14:34:28 -0800282 if (!IsStatic()) {
Ian Rogers0177e532014-02-11 16:30:46 -0800283 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800284 } else {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800285 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800286 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000287 if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200288 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700289 // exception was thrown to force the activations to be removed from the
290 // stack. Continue execution in the interpreter.
291 self->ClearException();
Sebastien Hertzf7958692015-06-09 14:09:14 +0200292 ShadowFrame* shadow_frame =
293 self->PopStackedShadowFrame(StackedShadowFrameType::kDeoptimizationShadowFrame);
Sebastien Hertz07474662015-08-25 15:12:33 +0000294 mirror::Throwable* pending_exception = nullptr;
295 self->PopDeoptimizationContext(result, &pending_exception);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700296 self->SetTopOfStack(nullptr);
Jeff Hao790ad902013-05-22 15:02:08 -0700297 self->SetTopOfShadowStack(shadow_frame);
Sebastien Hertz07474662015-08-25 15:12:33 +0000298
299 // Restore the exception that was pending before deoptimization then interpret the
300 // deoptimized frames.
301 if (pending_exception != nullptr) {
302 self->SetException(pending_exception);
303 }
Jeff Hao790ad902013-05-22 15:02:08 -0700304 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
305 }
306 if (kLogInvocationStartAndReturn) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800307 LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod(this).c_str(),
308 GetEntryPointFromQuickCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800309 }
310 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800311 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Ian Rogersf2247512014-12-02 16:17:08 -0800312 if (result != nullptr) {
Jeff Hao5d917302013-02-27 17:57:33 -0800313 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 }
315 }
316 }
317
318 // Pop transition.
319 self->PopManagedStackFragment(fragment);
320}
321
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700322void ArtMethod::RegisterNative(const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700324 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogersf2247512014-12-02 16:17:08 -0800325 CHECK(native_method != nullptr) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700326 if (is_fast) {
327 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800329 SetEntryPointFromJni(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330}
331
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700332void ArtMethod::UnregisterNative() {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700333 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 // restore stub to lookup native pointer via dlsym
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700335 RegisterNative(GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336}
337
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700338bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
339 auto* dex_cache = GetDexCache();
340 auto* dex_file = dex_cache->GetDexFile();
341 const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
342 const auto& proto_id = dex_file->GetMethodPrototype(method_id);
343 const DexFile::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
344 auto count = proto_params != nullptr ? proto_params->Size() : 0u;
345 auto param_len = params.Get() != nullptr ? params->GetLength() : 0u;
346 if (param_len != count) {
347 return false;
348 }
349 auto* cl = Runtime::Current()->GetClassLinker();
350 for (size_t i = 0; i < count; ++i) {
351 auto type_idx = proto_params->GetTypeItem(i).type_idx_;
352 auto* type = cl->ResolveType(type_idx, this);
353 if (type == nullptr) {
354 Thread::Current()->AssertPendingException();
355 return false;
356 }
357 if (type != params->GetWithoutChecks(i)) {
358 return false;
359 }
360 }
361 return true;
362}
363
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100364ProfilingInfo* ArtMethod::CreateProfilingInfo() {
Mathieu Chartier1147b9b2015-09-14 18:50:08 -0700365 DCHECK(!Runtime::Current()->IsAotCompiler());
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100366 ProfilingInfo* info = ProfilingInfo::Create(this);
367 MemberOffset offset = ArtMethod::EntryPointFromJniOffset(sizeof(void*));
368 uintptr_t pointer = reinterpret_cast<uintptr_t>(this) + offset.Uint32Value();
369 if (!reinterpret_cast<Atomic<ProfilingInfo*>*>(pointer)->
370 CompareExchangeStrongSequentiallyConsistent(nullptr, info)) {
Mathieu Chartier1147b9b2015-09-14 18:50:08 -0700371 return GetProfilingInfo(sizeof(void*));
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100372 } else {
373 return info;
374 }
375}
376
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100377const uint8_t* ArtMethod::GetQuickenedInfo() {
378 bool found = false;
379 OatFile::OatMethod oat_method =
380 Runtime::Current()->GetClassLinker()->FindOatMethodFor(this, &found);
381 if (!found || (oat_method.GetQuickCode() != nullptr)) {
382 return nullptr;
383 }
384 return oat_method.GetVmapTable();
385}
386
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100387const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) {
388 if (IsRuntimeMethod() || IsProxyMethod()) {
389 return nullptr;
390 }
391
392 Runtime* runtime = Runtime::Current();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100393 const void* existing_entry_point = GetEntryPointFromQuickCompiledCode();
394 DCHECK(existing_entry_point != nullptr);
395 ClassLinker* class_linker = runtime->GetClassLinker();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100396
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100397 if (class_linker->IsQuickGenericJniStub(existing_entry_point)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100398 // The generic JNI does not have any method header.
399 return nullptr;
400 }
401
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100402 // Check whether the current entry point contains this pc.
403 if (!class_linker->IsQuickResolutionStub(existing_entry_point) &&
404 !class_linker->IsQuickToInterpreterBridge(existing_entry_point)) {
405 OatQuickMethodHeader* method_header =
406 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100407
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100408 if (method_header->Contains(pc)) {
409 return method_header;
410 }
411 }
412
413 // Check whether the pc is in the JIT code cache.
414 jit::Jit* jit = Runtime::Current()->GetJit();
415 if (jit != nullptr) {
416 jit::JitCodeCache* code_cache = jit->GetCodeCache();
417 OatQuickMethodHeader* method_header = code_cache->LookupMethodHeader(pc, this);
418 if (method_header != nullptr) {
419 DCHECK(method_header->Contains(pc));
420 return method_header;
421 } else {
422 DCHECK(!code_cache->ContainsPc(reinterpret_cast<const void*>(pc))) << std::hex << pc;
423 }
424 }
425
426 // The code has to be in an oat file.
427 bool found;
428 OatFile::OatMethod oat_method = class_linker->FindOatMethodFor(this, &found);
429 if (!found) {
430 // Only for unit tests.
431 // TODO(ngeoffray): Update these tests to pass the right pc?
432 return OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
433 }
434 const void* oat_entry_point = oat_method.GetQuickCode();
435 if (oat_entry_point == nullptr || class_linker->IsQuickGenericJniStub(oat_entry_point)) {
436 DCHECK(IsNative());
437 return nullptr;
438 }
439
440 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromEntryPoint(oat_entry_point);
441 if (pc == 0) {
442 // This is a downcall, it can only happen for a native method.
443 DCHECK(IsNative());
444 return method_header;
445 }
446
447 if (pc == reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc())) {
448 // If we're instrumenting, just return the compiled OAT code.
449 // TODO(ngeoffray): Avoid this call path.
450 return method_header;
451 }
452
453 DCHECK(method_header->Contains(pc))
454 << PrettyMethod(this)
455 << std::hex << pc << " " << oat_entry_point
456 << " " << (uintptr_t)(method_header->code_ + method_header->code_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100457 return method_header;
458}
459
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800460} // namespace art