blob: d812590cc72c2475e285242a62d10dac5b5d4bdd [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
Andreas Gampe479b1de2016-07-19 18:27:17 -070019#include <cstddef>
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "arch/context.h"
Ian Rogers62f05122014-03-21 11:21:29 -070022#include "art_field-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "base/stringpiece.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070025#include "class_linker-inl.h"
Andreas Gampe2a5c4682015-08-14 08:22:54 -070026#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Ian Rogersc449aa82013-07-29 14:35:46 -070028#include "dex_instruction.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070029#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "jit/jit.h"
33#include "jit/jit_code_cache.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010034#include "jit/profiling_info.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "jni_internal.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
Alex Light9139e002015-10-09 15:59:48 -070070void ArtMethod::ThrowInvocationTimeError() {
71 DCHECK(!IsInvokable());
72 // NOTE: IsDefaultConflicting must be first since the actual method might or might not be abstract
73 // due to the way we select it.
74 if (IsDefaultConflicting()) {
75 ThrowIncompatibleClassChangeErrorForMethodConflict(this);
76 } else {
77 DCHECK(IsAbstract());
78 ThrowAbstractMethodError(this);
79 }
80}
81
Ian Rogersef7d42f2014-01-06 12:55:46 -080082InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083 // TODO: kSuper?
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +000084 if (IsStatic()) {
Nicolas Geoffray12abcbd2016-06-06 15:51:58 +000085 return kStatic;
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +000086 } else if (GetDeclaringClass()->IsInterface()) {
87 return kInterface;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 } else if (IsDirect()) {
89 return kDirect;
90 } else {
91 return kVirtual;
92 }
93}
94
Brian Carlstromea46f952013-07-30 01:26:50 -070095size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -070096 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -070098 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 char ch = shorty[i];
100 if (ch == 'D' || ch == 'J') {
101 num_registers += 2;
102 } else {
103 num_registers += 1;
104 }
105 }
106 return num_registers;
107}
108
Alex Light6c8467f2015-11-20 15:03:26 -0800109bool ArtMethod::HasSameNameAndSignature(ArtMethod* other) {
Ian Rogersf2247512014-12-02 16:17:08 -0800110 ScopedAssertNoThreadSuspension ants(Thread::Current(), "HasSameNameAndSignature");
Alex Light6c8467f2015-11-20 15:03:26 -0800111 const DexFile* dex_file = GetDexFile();
112 const DexFile::MethodId& mid = dex_file->GetMethodId(GetDexMethodIndex());
113 if (GetDexCache() == other->GetDexCache()) {
114 const DexFile::MethodId& mid2 = dex_file->GetMethodId(other->GetDexMethodIndex());
Ian Rogersf2247512014-12-02 16:17:08 -0800115 return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
116 }
Alex Light6c8467f2015-11-20 15:03:26 -0800117 const DexFile* dex_file2 = other->GetDexFile();
118 const DexFile::MethodId& mid2 = dex_file2->GetMethodId(other->GetDexMethodIndex());
Ian Rogersf2247512014-12-02 16:17:08 -0800119 if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
120 return false; // Name mismatch.
121 }
122 return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
123}
124
Andreas Gampe542451c2016-07-26 09:02:02 -0700125ArtMethod* ArtMethod::FindOverriddenMethod(PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 if (IsStatic()) {
Ian Rogersf2247512014-12-02 16:17:08 -0800127 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700129 mirror::Class* declaring_class = GetDeclaringClass();
130 mirror::Class* super_class = declaring_class->GetSuperClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 uint16_t method_index = GetMethodIndex();
Ian Rogersf2247512014-12-02 16:17:08 -0800132 ArtMethod* result = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 // Did this method override a super class method? If so load the result from the super class'
134 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700135 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700136 result = super_class->GetVTableEntry(method_index, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800137 } else {
138 // Method didn't override superclass method so search interfaces
139 if (IsProxyMethod()) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100140 result = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
141 GetDexMethodIndex(),
142 pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 CHECK_EQ(result,
144 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
145 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700146 mirror::IfTable* iftable = GetDeclaringClass()->GetIfTable();
Ian Rogersf2247512014-12-02 16:17:08 -0800147 for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 mirror::Class* interface = iftable->GetInterface(i);
Alex Light51a64d52015-12-17 13:55:59 -0800149 for (ArtMethod& interface_method : interface->GetVirtualMethods(pointer_size)) {
150 if (HasSameNameAndSignature(interface_method.GetInterfaceMethodIfProxy(pointer_size))) {
151 result = &interface_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 break;
153 }
154 }
155 }
156 }
157 }
Alex Light6c8467f2015-11-20 15:03:26 -0800158 DCHECK(result == nullptr ||
Alex Light51a64d52015-12-17 13:55:59 -0800159 GetInterfaceMethodIfProxy(pointer_size)->HasSameNameAndSignature(
160 result->GetInterfaceMethodIfProxy(pointer_size)));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 return result;
162}
163
Ian Rogerse0a02da2014-12-02 14:10:53 -0800164uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
165 uint32_t name_and_signature_idx) {
166 const DexFile* dexfile = GetDexFile();
167 const uint32_t dex_method_idx = GetDexMethodIndex();
168 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
169 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
170 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
171 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
172 if (dexfile == &other_dexfile) {
173 return dex_method_idx;
174 }
175 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700176 const DexFile::TypeId* other_type_id = other_dexfile.FindTypeId(mid_declaring_class_descriptor);
177 if (other_type_id != nullptr) {
178 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
179 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
180 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
181 if (other_mid != nullptr) {
182 return other_dexfile.GetIndexForMethodId(*other_mid);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800183 }
184 }
185 return DexFile::kDexNoIndex;
186}
187
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700189 uint32_t dex_pc, bool* has_no_move_exception) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700190 const DexFile::CodeItem* code_item = GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700191 // Set aside the exception while we resolve its type.
192 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700193 StackHandleScope<1> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000194 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Jeff Haoaa961912014-04-22 13:54:32 -0700195 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700196 // Default to handler not found.
197 uint32_t found_dex_pc = DexFile::kDexNoIndex;
198 // Iterate over the catch handlers associated with dex_pc.
Andreas Gampe542451c2016-07-26 09:02:02 -0700199 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
201 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
202 // Catch all case
203 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700204 found_dex_pc = it.GetHandlerAddress();
205 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206 }
207 // Does this catch exception type apply?
Vladimir Marko05792b92015-08-03 11:56:49 +0100208 mirror::Class* iter_exception_type = GetClassFromTypeIndex(iter_type_idx,
209 true /* resolve */,
210 pointer_size);
Ian Rogers822266b2014-05-29 16:55:06 -0700211 if (UNLIKELY(iter_exception_type == nullptr)) {
212 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
213 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700214 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700215 self->ClearException();
216 // Delete any long jump context as this routine is called during a stack walk which will
217 // release its in use context at the end.
218 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700220 << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700221 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700222 found_dex_pc = it.GetHandlerAddress();
223 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 }
225 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700226 if (found_dex_pc != DexFile::kDexNoIndex) {
227 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700228 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700229 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
230 }
Jeff Haoaa961912014-04-22 13:54:32 -0700231 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700232 if (exception.Get() != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000233 self->SetException(exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700234 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700235 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236}
237
Brian Carlstromea46f952013-07-30 01:26:50 -0700238void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800239 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700240 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
241 ThrowStackOverflowError(self);
242 return;
243 }
244
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245 if (kIsDebugBuild) {
246 self->AssertThreadSuspensionIsAllowable();
247 CHECK_EQ(kRunnable, self->GetState());
Andreas Gampe542451c2016-07-26 09:02:02 -0700248 CHECK_STREQ(GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 }
250
251 // Push a transition back into managed code onto the linked list in thread.
252 ManagedStack fragment;
253 self->PushManagedStackFragment(&fragment);
254
Ian Rogers62d6c772013-02-27 08:32:07 -0800255 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700256 // Call the invoke stub, passing everything as arguments.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200257 // If the runtime is not yet started or it is required by the debugger, then perform the
Aart Bik01223202016-05-05 15:10:42 -0700258 // Invocation by the interpreter, explicitly forcing interpretation over JIT to prevent
259 // cycling around the various JIT/Interpreter methods that handle method invocation.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200260 if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this))) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700261 if (IsStatic()) {
Aart Bik01223202016-05-05 15:10:42 -0700262 art::interpreter::EnterInterpreterFromInvoke(
263 self, this, nullptr, args, result, /*stay_in_interpreter*/ true);
Ian Rogers5d27faf2014-05-02 17:17:18 -0700264 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 mirror::Object* receiver =
266 reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
Aart Bik01223202016-05-05 15:10:42 -0700267 art::interpreter::EnterInterpreterFromInvoke(
268 self, this, receiver, args + 1, result, /*stay_in_interpreter*/ true);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 }
270 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700271 DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272
273 constexpr bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800274 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800275 if (LIKELY(have_quick_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700276 if (kLogInvocationStartAndReturn) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 LOG(INFO) << StringPrintf(
278 "Invoking '%s' quick code=%p static=%d", PrettyMethod(this).c_str(),
279 GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0));
Jeff Hao790ad902013-05-22 15:02:08 -0700280 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700281
Elliott Hughes956af0f2014-12-11 14:34:28 -0800282 // Ensure that we won't be accidentally calling quick compiled code when -Xint.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800283 if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100284 CHECK(!runtime->UseJitCompilation());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100285 const void* oat_quick_code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(this);
286 CHECK(oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode())
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700287 << "Don't call compiled code when -Xint " << PrettyMethod(this);
288 }
289
Elliott Hughes956af0f2014-12-11 14:34:28 -0800290 if (!IsStatic()) {
Ian Rogers0177e532014-02-11 16:30:46 -0800291 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800292 } else {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800293 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800294 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000295 if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200296 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700297 // exception was thrown to force the activations to be removed from the
298 // stack. Continue execution in the interpreter.
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000299 self->DeoptimizeWithDeoptimizationException(result);
Jeff Hao790ad902013-05-22 15:02:08 -0700300 }
301 if (kLogInvocationStartAndReturn) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800302 LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod(this).c_str(),
303 GetEntryPointFromQuickCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800304 }
305 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800306 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Ian Rogersf2247512014-12-02 16:17:08 -0800307 if (result != nullptr) {
Jeff Hao5d917302013-02-27 17:57:33 -0800308 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800309 }
310 }
311 }
312
313 // Pop transition.
314 self->PopManagedStackFragment(fragment);
315}
316
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700317void ArtMethod::RegisterNative(const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700319 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogersf2247512014-12-02 16:17:08 -0800320 CHECK(native_method != nullptr) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700321 if (is_fast) {
322 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800324 SetEntryPointFromJni(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800325}
326
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700327void ArtMethod::UnregisterNative() {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700328 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329 // restore stub to lookup native pointer via dlsym
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700330 RegisterNative(GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331}
332
Alex Light9139e002015-10-09 15:59:48 -0700333bool ArtMethod::IsOverridableByDefaultMethod() {
334 return GetDeclaringClass()->IsInterface();
335}
336
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700337bool ArtMethod::IsAnnotatedWithFastNative() {
338 Thread* self = Thread::Current();
339 ScopedObjectAccess soa(self);
340 StackHandleScope<1> shs(self);
341
342 const DexFile& dex_file = GetDeclaringClass()->GetDexFile();
343
344 mirror::Class* fast_native_annotation =
345 soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_annotation_optimization_FastNative);
346 Handle<mirror::Class> fast_native_handle(shs.NewHandle(fast_native_annotation));
347
348 // Note: Resolves any method annotations' classes as a side-effect.
349 // -- This seems allowed by the spec since it says we can preload any classes
350 // referenced by another classes's constant pool table.
351 return dex_file.IsMethodAnnotationPresent(this, fast_native_handle, DexFile::kDexVisibilityBuild);
352}
353
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700354bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
355 auto* dex_cache = GetDexCache();
356 auto* dex_file = dex_cache->GetDexFile();
357 const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
358 const auto& proto_id = dex_file->GetMethodPrototype(method_id);
359 const DexFile::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
360 auto count = proto_params != nullptr ? proto_params->Size() : 0u;
361 auto param_len = params.Get() != nullptr ? params->GetLength() : 0u;
362 if (param_len != count) {
363 return false;
364 }
365 auto* cl = Runtime::Current()->GetClassLinker();
366 for (size_t i = 0; i < count; ++i) {
367 auto type_idx = proto_params->GetTypeItem(i).type_idx_;
368 auto* type = cl->ResolveType(type_idx, this);
369 if (type == nullptr) {
370 Thread::Current()->AssertPendingException();
371 return false;
372 }
373 if (type != params->GetWithoutChecks(i)) {
374 return false;
375 }
376 }
377 return true;
378}
379
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100380const uint8_t* ArtMethod::GetQuickenedInfo() {
381 bool found = false;
382 OatFile::OatMethod oat_method =
383 Runtime::Current()->GetClassLinker()->FindOatMethodFor(this, &found);
384 if (!found || (oat_method.GetQuickCode() != nullptr)) {
385 return nullptr;
386 }
387 return oat_method.GetVmapTable();
388}
389
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100390const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) {
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000391 // Our callers should make sure they don't pass the instrumentation exit pc,
392 // as this method does not look at the side instrumentation stack.
393 DCHECK_NE(pc, reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()));
394
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000395 if (IsRuntimeMethod()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100396 return nullptr;
397 }
398
399 Runtime* runtime = Runtime::Current();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100400 const void* existing_entry_point = GetEntryPointFromQuickCompiledCode();
Nicolas Geoffrayde166542016-03-07 15:44:08 +0000401 CHECK(existing_entry_point != nullptr) << PrettyMethod(this) << "@" << this;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100402 ClassLinker* class_linker = runtime->GetClassLinker();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100403
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100404 if (class_linker->IsQuickGenericJniStub(existing_entry_point)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100405 // The generic JNI does not have any method header.
406 return nullptr;
407 }
408
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000409 if (existing_entry_point == GetQuickProxyInvokeHandler()) {
410 DCHECK(IsProxyMethod() && !IsConstructor());
411 // The proxy entry point does not have any method header.
412 return nullptr;
413 }
414
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100415 // Check whether the current entry point contains this pc.
416 if (!class_linker->IsQuickResolutionStub(existing_entry_point) &&
417 !class_linker->IsQuickToInterpreterBridge(existing_entry_point)) {
418 OatQuickMethodHeader* method_header =
419 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100420
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100421 if (method_header->Contains(pc)) {
422 return method_header;
423 }
424 }
425
426 // Check whether the pc is in the JIT code cache.
427 jit::Jit* jit = Runtime::Current()->GetJit();
428 if (jit != nullptr) {
429 jit::JitCodeCache* code_cache = jit->GetCodeCache();
430 OatQuickMethodHeader* method_header = code_cache->LookupMethodHeader(pc, this);
431 if (method_header != nullptr) {
432 DCHECK(method_header->Contains(pc));
433 return method_header;
434 } else {
Nicolas Geoffray2a524bd2016-03-01 12:18:47 +0000435 DCHECK(!code_cache->ContainsPc(reinterpret_cast<const void*>(pc)))
436 << PrettyMethod(this)
437 << ", pc=" << std::hex << pc
438 << ", entry_point=" << std::hex << reinterpret_cast<uintptr_t>(existing_entry_point)
439 << ", copy=" << std::boolalpha << IsCopied()
440 << ", proxy=" << std::boolalpha << IsProxyMethod();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100441 }
442 }
443
444 // The code has to be in an oat file.
445 bool found;
446 OatFile::OatMethod oat_method = class_linker->FindOatMethodFor(this, &found);
447 if (!found) {
Nicolas Geoffray49e43962015-10-28 16:16:16 +0000448 if (class_linker->IsQuickResolutionStub(existing_entry_point)) {
449 // We are running the generic jni stub, but the entry point of the method has not
450 // been updated yet.
Nicolas Geoffray703c2822015-10-30 12:23:16 +0000451 DCHECK_EQ(pc, 0u) << "Should be a downcall";
452 DCHECK(IsNative());
453 return nullptr;
454 }
455 if (existing_entry_point == GetQuickInstrumentationEntryPoint()) {
456 // We are running the generic jni stub, but the method is being instrumented.
457 DCHECK_EQ(pc, 0u) << "Should be a downcall";
Nicolas Geoffray49e43962015-10-28 16:16:16 +0000458 DCHECK(IsNative());
459 return nullptr;
460 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100461 // Only for unit tests.
462 // TODO(ngeoffray): Update these tests to pass the right pc?
463 return OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
464 }
465 const void* oat_entry_point = oat_method.GetQuickCode();
466 if (oat_entry_point == nullptr || class_linker->IsQuickGenericJniStub(oat_entry_point)) {
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000467 DCHECK(IsNative()) << PrettyMethod(this);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100468 return nullptr;
469 }
470
471 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromEntryPoint(oat_entry_point);
472 if (pc == 0) {
473 // This is a downcall, it can only happen for a native method.
474 DCHECK(IsNative());
475 return method_header;
476 }
477
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100478 DCHECK(method_header->Contains(pc))
479 << PrettyMethod(this)
480 << std::hex << pc << " " << oat_entry_point
481 << " " << (uintptr_t)(method_header->code_ + method_header->code_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100482 return method_header;
483}
484
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000485bool ArtMethod::HasAnyCompiledCode() {
486 // Check whether the JIT has compiled it.
487 jit::Jit* jit = Runtime::Current()->GetJit();
488 if (jit != nullptr && jit->GetCodeCache()->ContainsMethod(this)) {
489 return true;
490 }
491
492 // Check whether we have AOT code.
493 return Runtime::Current()->GetClassLinker()->GetOatMethodQuickCodeFor(this) != nullptr;
494}
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000495
Andreas Gampe542451c2016-07-26 09:02:02 -0700496void ArtMethod::CopyFrom(ArtMethod* src, PointerSize image_pointer_size) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000497 memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src),
498 Size(image_pointer_size));
499 declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass());
500
501 // If the entry point of the method we are copying from is from JIT code, we just
502 // put the entry point of the new method to interpreter. We could set the entry point
503 // to the JIT code, but this would require taking the JIT code cache lock to notify
504 // it, which we do not want at this level.
505 Runtime* runtime = Runtime::Current();
Calin Juravleffc87072016-04-20 14:22:09 +0100506 if (runtime->UseJitCompilation()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000507 if (runtime->GetJit()->GetCodeCache()->ContainsPc(GetEntryPointFromQuickCompiledCode())) {
508 SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), image_pointer_size);
509 }
510 }
511 // Clear the profiling info for the same reasons as the JIT code.
512 if (!src->IsNative()) {
513 SetProfilingInfoPtrSize(nullptr, image_pointer_size);
514 }
515 // Clear hotness to let the JIT properly decide when to compile this method.
516 hotness_count_ = 0;
517}
518
Andreas Gampe542451c2016-07-26 09:02:02 -0700519bool ArtMethod::IsImagePointerSize(PointerSize pointer_size) {
Andreas Gampe479b1de2016-07-19 18:27:17 -0700520 // Hijack this function to get access to PtrSizedFieldsOffset.
521 //
522 // Ensure that PrtSizedFieldsOffset is correct. We rely here on usually having both 32-bit and
523 // 64-bit builds.
524 static_assert(std::is_standard_layout<ArtMethod>::value, "ArtMethod is not standard layout.");
Andreas Gampe542451c2016-07-26 09:02:02 -0700525 static_assert(
526 (sizeof(void*) != 4) ||
527 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k32)),
528 "Unexpected 32-bit class layout.");
529 static_assert(
530 (sizeof(void*) != 8) ||
531 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k64)),
532 "Unexpected 64-bit class layout.");
Andreas Gampe479b1de2016-07-19 18:27:17 -0700533
Andreas Gampe75f08852016-07-19 08:06:07 -0700534 Runtime* runtime = Runtime::Current();
535 if (runtime == nullptr) {
536 return true;
537 }
538 return runtime->GetClassLinker()->GetImagePointerSize() == pointer_size;
539}
540
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541} // namespace art