blob: 78eea185a7a0278bc11e184ae19c78bda4f45a6d [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"
David Sehr9323e6e2016-09-13 08:58:35 -070028#include "dex_file_annotations.h"
Ian Rogersc449aa82013-07-29 14:35:46 -070029#include "dex_instruction.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070030#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "interpreter/interpreter.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080033#include "jit/jit.h"
34#include "jit/jit_code_cache.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010035#include "jit/profiling_info.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "jni_internal.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/abstract_method.h"
38#include "mirror/class-inl.h"
39#include "mirror/object_array-inl.h"
40#include "mirror/object-inl.h"
41#include "mirror/string.h"
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000042#include "oat_file-inl.h"
Ian Rogers62f05122014-03-21 11:21:29 -070043#include "scoped_thread_state_change.h"
Ian Rogers62f05122014-03-21 11:21:29 -070044#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045
46namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047
Ian Rogers0177e532014-02-11 16:30:46 -080048extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
49 const char*);
Ian Rogers936b37f2014-02-14 00:52:24 -080050extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
51 const char*);
Jeff Hao5d917302013-02-27 17:57:33 -080052
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070053ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
54 jobject jlr_method) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -070055 auto* abstract_method = soa.Decode<mirror::AbstractMethod*>(jlr_method);
56 DCHECK(abstract_method != nullptr);
57 return abstract_method->GetArtMethod();
Ian Rogers62f05122014-03-21 11:21:29 -070058}
59
Ian Rogers6b14d552014-10-28 21:50:58 -070060mirror::String* ArtMethod::GetNameAsString(Thread* self) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070061 CHECK(!IsProxyMethod());
Ian Rogers6b14d552014-10-28 21:50:58 -070062 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
64 auto* dex_file = dex_cache->GetDexFile();
65 uint32_t dex_method_idx = GetDexMethodIndex();
66 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
Ian Rogers6b14d552014-10-28 21:50:58 -070067 return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
68 dex_cache);
69}
70
Alex Light9139e002015-10-09 15:59:48 -070071void ArtMethod::ThrowInvocationTimeError() {
72 DCHECK(!IsInvokable());
73 // NOTE: IsDefaultConflicting must be first since the actual method might or might not be abstract
74 // due to the way we select it.
75 if (IsDefaultConflicting()) {
76 ThrowIncompatibleClassChangeErrorForMethodConflict(this);
77 } else {
78 DCHECK(IsAbstract());
79 ThrowAbstractMethodError(this);
80 }
81}
82
Ian Rogersef7d42f2014-01-06 12:55:46 -080083InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 // TODO: kSuper?
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +000085 if (IsStatic()) {
Nicolas Geoffray12abcbd2016-06-06 15:51:58 +000086 return kStatic;
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +000087 } else if (GetDeclaringClass()->IsInterface()) {
88 return kInterface;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 } else if (IsDirect()) {
90 return kDirect;
91 } else {
92 return kVirtual;
93 }
94}
95
Brian Carlstromea46f952013-07-30 01:26:50 -070096size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers6b604a12014-09-25 15:35:37 -070097 CHECK_LE(1U, shorty.length());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 uint32_t num_registers = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -070099 for (size_t i = 1; i < shorty.length(); ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 char ch = shorty[i];
101 if (ch == 'D' || ch == 'J') {
102 num_registers += 2;
103 } else {
104 num_registers += 1;
105 }
106 }
107 return num_registers;
108}
109
Alex Light6c8467f2015-11-20 15:03:26 -0800110bool ArtMethod::HasSameNameAndSignature(ArtMethod* other) {
Ian Rogersf2247512014-12-02 16:17:08 -0800111 ScopedAssertNoThreadSuspension ants(Thread::Current(), "HasSameNameAndSignature");
Alex Light6c8467f2015-11-20 15:03:26 -0800112 const DexFile* dex_file = GetDexFile();
113 const DexFile::MethodId& mid = dex_file->GetMethodId(GetDexMethodIndex());
114 if (GetDexCache() == other->GetDexCache()) {
115 const DexFile::MethodId& mid2 = dex_file->GetMethodId(other->GetDexMethodIndex());
Ian Rogersf2247512014-12-02 16:17:08 -0800116 return mid.name_idx_ == mid2.name_idx_ && mid.proto_idx_ == mid2.proto_idx_;
117 }
Alex Light6c8467f2015-11-20 15:03:26 -0800118 const DexFile* dex_file2 = other->GetDexFile();
119 const DexFile::MethodId& mid2 = dex_file2->GetMethodId(other->GetDexMethodIndex());
Ian Rogersf2247512014-12-02 16:17:08 -0800120 if (!DexFileStringEquals(dex_file, mid.name_idx_, dex_file2, mid2.name_idx_)) {
121 return false; // Name mismatch.
122 }
123 return dex_file->GetMethodSignature(mid) == dex_file2->GetMethodSignature(mid2);
124}
125
Andreas Gampe542451c2016-07-26 09:02:02 -0700126ArtMethod* ArtMethod::FindOverriddenMethod(PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 if (IsStatic()) {
Ian Rogersf2247512014-12-02 16:17:08 -0800128 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130 mirror::Class* declaring_class = GetDeclaringClass();
131 mirror::Class* super_class = declaring_class->GetSuperClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800132 uint16_t method_index = GetMethodIndex();
Ian Rogersf2247512014-12-02 16:17:08 -0800133 ArtMethod* result = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 // Did this method override a super class method? If so load the result from the super class'
135 // vtable
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700136 if (super_class->HasVTable() && method_index < super_class->GetVTableLength()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700137 result = super_class->GetVTableEntry(method_index, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 } else {
139 // Method didn't override superclass method so search interfaces
140 if (IsProxyMethod()) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100141 result = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
142 GetDexMethodIndex(),
143 pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 CHECK_EQ(result,
145 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
146 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700147 mirror::IfTable* iftable = GetDeclaringClass()->GetIfTable();
Ian Rogersf2247512014-12-02 16:17:08 -0800148 for (size_t i = 0; i < iftable->Count() && result == nullptr; i++) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700149 mirror::Class* interface = iftable->GetInterface(i);
Alex Light51a64d52015-12-17 13:55:59 -0800150 for (ArtMethod& interface_method : interface->GetVirtualMethods(pointer_size)) {
151 if (HasSameNameAndSignature(interface_method.GetInterfaceMethodIfProxy(pointer_size))) {
152 result = &interface_method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 break;
154 }
155 }
156 }
157 }
158 }
Alex Light6c8467f2015-11-20 15:03:26 -0800159 DCHECK(result == nullptr ||
Alex Light51a64d52015-12-17 13:55:59 -0800160 GetInterfaceMethodIfProxy(pointer_size)->HasSameNameAndSignature(
161 result->GetInterfaceMethodIfProxy(pointer_size)));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 return result;
163}
164
Ian Rogerse0a02da2014-12-02 14:10:53 -0800165uint32_t ArtMethod::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
166 uint32_t name_and_signature_idx) {
167 const DexFile* dexfile = GetDexFile();
168 const uint32_t dex_method_idx = GetDexMethodIndex();
169 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
170 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
171 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
172 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
173 if (dexfile == &other_dexfile) {
174 return dex_method_idx;
175 }
176 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700177 const DexFile::TypeId* other_type_id = other_dexfile.FindTypeId(mid_declaring_class_descriptor);
178 if (other_type_id != nullptr) {
179 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
180 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
181 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
182 if (other_mid != nullptr) {
183 return other_dexfile.GetIndexForMethodId(*other_mid);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800184 }
185 }
186 return DexFile::kDexNoIndex;
187}
188
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189uint32_t ArtMethod::FindCatchBlock(Handle<mirror::Class> exception_type,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700190 uint32_t dex_pc, bool* has_no_move_exception) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700191 const DexFile::CodeItem* code_item = GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700192 // Set aside the exception while we resolve its type.
193 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700194 StackHandleScope<1> hs(self);
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000195 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException()));
Jeff Haoaa961912014-04-22 13:54:32 -0700196 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700197 // Default to handler not found.
198 uint32_t found_dex_pc = DexFile::kDexNoIndex;
199 // Iterate over the catch handlers associated with dex_pc.
Andreas Gampe542451c2016-07-26 09:02:02 -0700200 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
202 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
203 // Catch all case
204 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700205 found_dex_pc = it.GetHandlerAddress();
206 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 }
208 // Does this catch exception type apply?
Vladimir Marko05792b92015-08-03 11:56:49 +0100209 mirror::Class* iter_exception_type = GetClassFromTypeIndex(iter_type_idx,
210 true /* resolve */,
211 pointer_size);
Ian Rogers822266b2014-05-29 16:55:06 -0700212 if (UNLIKELY(iter_exception_type == nullptr)) {
213 // Now have a NoClassDefFoundError as exception. Ignore in case the exception class was
214 // removed by a pro-guard like tool.
Andreas Gampe72b3e432014-05-13 21:42:05 -0700215 // Note: this is not RI behavior. RI would have failed when loading the class.
Ian Rogers822266b2014-05-29 16:55:06 -0700216 self->ClearException();
217 // Delete any long jump context as this routine is called during a stack walk which will
218 // release its in use context at the end.
219 delete self->GetLongJumpContext();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Mathieu Chartiere401d142015-04-22 13:56:20 -0700221 << DescriptorToDot(GetTypeDescriptorFromTypeIdx(iter_type_idx));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700222 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700223 found_dex_pc = it.GetHandlerAddress();
224 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 }
226 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700227 if (found_dex_pc != DexFile::kDexNoIndex) {
228 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700229 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700230 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
231 }
Jeff Haoaa961912014-04-22 13:54:32 -0700232 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700233 if (exception.Get() != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000234 self->SetException(exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700235 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700236 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237}
238
Brian Carlstromea46f952013-07-30 01:26:50 -0700239void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800240 const char* shorty) {
Dave Allison648d7112014-07-25 16:15:27 -0700241 if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
242 ThrowStackOverflowError(self);
243 return;
244 }
245
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 if (kIsDebugBuild) {
247 self->AssertThreadSuspensionIsAllowable();
248 CHECK_EQ(kRunnable, self->GetState());
Andreas Gampe542451c2016-07-26 09:02:02 -0700249 CHECK_STREQ(GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250 }
251
252 // Push a transition back into managed code onto the linked list in thread.
253 ManagedStack fragment;
254 self->PushManagedStackFragment(&fragment);
255
Ian Rogers62d6c772013-02-27 08:32:07 -0800256 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700257 // Call the invoke stub, passing everything as arguments.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200258 // If the runtime is not yet started or it is required by the debugger, then perform the
Aart Bik01223202016-05-05 15:10:42 -0700259 // Invocation by the interpreter, explicitly forcing interpretation over JIT to prevent
260 // cycling around the various JIT/Interpreter methods that handle method invocation.
Daniel Mihalyieb076692014-08-22 17:33:31 +0200261 if (UNLIKELY(!runtime->IsStarted() || Dbg::IsForcedInterpreterNeededForCalling(self, this))) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700262 if (IsStatic()) {
Aart Bik01223202016-05-05 15:10:42 -0700263 art::interpreter::EnterInterpreterFromInvoke(
264 self, this, nullptr, args, result, /*stay_in_interpreter*/ true);
Ian Rogers5d27faf2014-05-02 17:17:18 -0700265 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266 mirror::Object* receiver =
267 reinterpret_cast<StackReference<mirror::Object>*>(&args[0])->AsMirrorPtr();
Aart Bik01223202016-05-05 15:10:42 -0700268 art::interpreter::EnterInterpreterFromInvoke(
269 self, this, receiver, args + 1, result, /*stay_in_interpreter*/ true);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 }
271 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700272 DCHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700273
274 constexpr bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800275 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
Elliott Hughes956af0f2014-12-11 14:34:28 -0800276 if (LIKELY(have_quick_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700277 if (kLogInvocationStartAndReturn) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 LOG(INFO) << StringPrintf(
279 "Invoking '%s' quick code=%p static=%d", PrettyMethod(this).c_str(),
280 GetEntryPointFromQuickCompiledCode(), static_cast<int>(IsStatic() ? 1 : 0));
Jeff Hao790ad902013-05-22 15:02:08 -0700281 }
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700282
Elliott Hughes956af0f2014-12-11 14:34:28 -0800283 // Ensure that we won't be accidentally calling quick compiled code when -Xint.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800284 if (kIsDebugBuild && runtime->GetInstrumentation()->IsForcedInterpretOnly()) {
Calin Juravleffc87072016-04-20 14:22:09 +0100285 CHECK(!runtime->UseJitCompilation());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100286 const void* oat_quick_code = runtime->GetClassLinker()->GetOatMethodQuickCodeFor(this);
287 CHECK(oat_quick_code == nullptr || oat_quick_code != GetEntryPointFromQuickCompiledCode())
Hiroshi Yamauchi9bdec882014-08-15 17:11:12 -0700288 << "Don't call compiled code when -Xint " << PrettyMethod(this);
289 }
290
Elliott Hughes956af0f2014-12-11 14:34:28 -0800291 if (!IsStatic()) {
Ian Rogers0177e532014-02-11 16:30:46 -0800292 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800293 } else {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800294 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800295 }
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000296 if (UNLIKELY(self->GetException() == Thread::GetDeoptimizationException())) {
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200297 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700298 // exception was thrown to force the activations to be removed from the
299 // stack. Continue execution in the interpreter.
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000300 self->DeoptimizeWithDeoptimizationException(result);
Jeff Hao790ad902013-05-22 15:02:08 -0700301 }
302 if (kLogInvocationStartAndReturn) {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800303 LOG(INFO) << StringPrintf("Returned '%s' quick code=%p", PrettyMethod(this).c_str(),
304 GetEntryPointFromQuickCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800305 }
306 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800307 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Ian Rogersf2247512014-12-02 16:17:08 -0800308 if (result != nullptr) {
Jeff Hao5d917302013-02-27 17:57:33 -0800309 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310 }
311 }
312 }
313
314 // Pop transition.
315 self->PopManagedStackFragment(fragment);
316}
317
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700318void ArtMethod::RegisterNative(const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700320 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogersf2247512014-12-02 16:17:08 -0800321 CHECK(native_method != nullptr) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700322 if (is_fast) {
323 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800325 SetEntryPointFromJni(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326}
327
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700328void ArtMethod::UnregisterNative() {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700329 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 // restore stub to lookup native pointer via dlsym
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700331 RegisterNative(GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332}
333
Alex Light9139e002015-10-09 15:59:48 -0700334bool ArtMethod::IsOverridableByDefaultMethod() {
335 return GetDeclaringClass()->IsInterface();
336}
337
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700338bool ArtMethod::IsAnnotatedWithFastNative() {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700339 return IsAnnotatedWith(WellKnownClasses::dalvik_annotation_optimization_FastNative,
340 DexFile::kDexVisibilityBuild);
341}
342
343bool ArtMethod::IsAnnotatedWithCriticalNative() {
344 return IsAnnotatedWith(WellKnownClasses::dalvik_annotation_optimization_CriticalNative,
345 DexFile::kDexVisibilityBuild);
346}
347
348bool ArtMethod::IsAnnotatedWith(jclass klass, uint32_t visibility) {
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700349 Thread* self = Thread::Current();
350 ScopedObjectAccess soa(self);
351 StackHandleScope<1> shs(self);
352
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700353 mirror::Class* annotation = soa.Decode<mirror::Class*>(klass);
354 DCHECK(annotation->IsAnnotation());
355 Handle<mirror::Class> annotation_handle(shs.NewHandle(annotation));
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700356
357 // Note: Resolves any method annotations' classes as a side-effect.
358 // -- This seems allowed by the spec since it says we can preload any classes
359 // referenced by another classes's constant pool table.
David Sehr9323e6e2016-09-13 08:58:35 -0700360 return annotations::IsMethodAnnotationPresent(this, annotation_handle, visibility);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700361}
362
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700363bool ArtMethod::EqualParameters(Handle<mirror::ObjectArray<mirror::Class>> params) {
364 auto* dex_cache = GetDexCache();
365 auto* dex_file = dex_cache->GetDexFile();
366 const auto& method_id = dex_file->GetMethodId(GetDexMethodIndex());
367 const auto& proto_id = dex_file->GetMethodPrototype(method_id);
368 const DexFile::TypeList* proto_params = dex_file->GetProtoParameters(proto_id);
369 auto count = proto_params != nullptr ? proto_params->Size() : 0u;
370 auto param_len = params.Get() != nullptr ? params->GetLength() : 0u;
371 if (param_len != count) {
372 return false;
373 }
374 auto* cl = Runtime::Current()->GetClassLinker();
375 for (size_t i = 0; i < count; ++i) {
376 auto type_idx = proto_params->GetTypeItem(i).type_idx_;
377 auto* type = cl->ResolveType(type_idx, this);
378 if (type == nullptr) {
379 Thread::Current()->AssertPendingException();
380 return false;
381 }
382 if (type != params->GetWithoutChecks(i)) {
383 return false;
384 }
385 }
386 return true;
387}
388
Nicolas Geoffray6bc43742015-10-12 18:11:10 +0100389const uint8_t* ArtMethod::GetQuickenedInfo() {
390 bool found = false;
391 OatFile::OatMethod oat_method =
392 Runtime::Current()->GetClassLinker()->FindOatMethodFor(this, &found);
393 if (!found || (oat_method.GetQuickCode() != nullptr)) {
394 return nullptr;
395 }
396 return oat_method.GetVmapTable();
397}
398
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100399const OatQuickMethodHeader* ArtMethod::GetOatQuickMethodHeader(uintptr_t pc) {
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000400 // Our callers should make sure they don't pass the instrumentation exit pc,
401 // as this method does not look at the side instrumentation stack.
402 DCHECK_NE(pc, reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()));
403
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000404 if (IsRuntimeMethod()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100405 return nullptr;
406 }
407
408 Runtime* runtime = Runtime::Current();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100409 const void* existing_entry_point = GetEntryPointFromQuickCompiledCode();
Nicolas Geoffrayde166542016-03-07 15:44:08 +0000410 CHECK(existing_entry_point != nullptr) << PrettyMethod(this) << "@" << this;
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100411 ClassLinker* class_linker = runtime->GetClassLinker();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100412
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100413 if (class_linker->IsQuickGenericJniStub(existing_entry_point)) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100414 // The generic JNI does not have any method header.
415 return nullptr;
416 }
417
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000418 if (existing_entry_point == GetQuickProxyInvokeHandler()) {
419 DCHECK(IsProxyMethod() && !IsConstructor());
420 // The proxy entry point does not have any method header.
421 return nullptr;
422 }
423
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100424 // Check whether the current entry point contains this pc.
425 if (!class_linker->IsQuickResolutionStub(existing_entry_point) &&
426 !class_linker->IsQuickToInterpreterBridge(existing_entry_point)) {
427 OatQuickMethodHeader* method_header =
428 OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100429
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100430 if (method_header->Contains(pc)) {
431 return method_header;
432 }
433 }
434
435 // Check whether the pc is in the JIT code cache.
436 jit::Jit* jit = Runtime::Current()->GetJit();
437 if (jit != nullptr) {
438 jit::JitCodeCache* code_cache = jit->GetCodeCache();
439 OatQuickMethodHeader* method_header = code_cache->LookupMethodHeader(pc, this);
440 if (method_header != nullptr) {
441 DCHECK(method_header->Contains(pc));
442 return method_header;
443 } else {
Nicolas Geoffray2a524bd2016-03-01 12:18:47 +0000444 DCHECK(!code_cache->ContainsPc(reinterpret_cast<const void*>(pc)))
445 << PrettyMethod(this)
446 << ", pc=" << std::hex << pc
447 << ", entry_point=" << std::hex << reinterpret_cast<uintptr_t>(existing_entry_point)
448 << ", copy=" << std::boolalpha << IsCopied()
449 << ", proxy=" << std::boolalpha << IsProxyMethod();
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100450 }
451 }
452
453 // The code has to be in an oat file.
454 bool found;
455 OatFile::OatMethod oat_method = class_linker->FindOatMethodFor(this, &found);
456 if (!found) {
Nicolas Geoffray49e43962015-10-28 16:16:16 +0000457 if (class_linker->IsQuickResolutionStub(existing_entry_point)) {
458 // We are running the generic jni stub, but the entry point of the method has not
459 // been updated yet.
Nicolas Geoffray703c2822015-10-30 12:23:16 +0000460 DCHECK_EQ(pc, 0u) << "Should be a downcall";
461 DCHECK(IsNative());
462 return nullptr;
463 }
464 if (existing_entry_point == GetQuickInstrumentationEntryPoint()) {
465 // We are running the generic jni stub, but the method is being instrumented.
466 DCHECK_EQ(pc, 0u) << "Should be a downcall";
Nicolas Geoffray49e43962015-10-28 16:16:16 +0000467 DCHECK(IsNative());
468 return nullptr;
469 }
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100470 // Only for unit tests.
471 // TODO(ngeoffray): Update these tests to pass the right pc?
472 return OatQuickMethodHeader::FromEntryPoint(existing_entry_point);
473 }
474 const void* oat_entry_point = oat_method.GetQuickCode();
475 if (oat_entry_point == nullptr || class_linker->IsQuickGenericJniStub(oat_entry_point)) {
Nicolas Geoffray5a23d2e2015-11-03 18:58:57 +0000476 DCHECK(IsNative()) << PrettyMethod(this);
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100477 return nullptr;
478 }
479
480 OatQuickMethodHeader* method_header = OatQuickMethodHeader::FromEntryPoint(oat_entry_point);
481 if (pc == 0) {
482 // This is a downcall, it can only happen for a native method.
483 DCHECK(IsNative());
484 return method_header;
485 }
486
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100487 DCHECK(method_header->Contains(pc))
488 << PrettyMethod(this)
Roland Levillain0b671c02016-08-19 12:02:34 +0100489 << " " << std::hex << pc << " " << oat_entry_point
Nicolas Geoffray1dad3f62015-10-23 14:59:54 +0100490 << " " << (uintptr_t)(method_header->code_ + method_header->code_size_);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100491 return method_header;
492}
493
Nicolas Geoffraya5891e82015-11-06 14:18:27 +0000494bool ArtMethod::HasAnyCompiledCode() {
495 // Check whether the JIT has compiled it.
496 jit::Jit* jit = Runtime::Current()->GetJit();
497 if (jit != nullptr && jit->GetCodeCache()->ContainsMethod(this)) {
498 return true;
499 }
500
501 // Check whether we have AOT code.
502 return Runtime::Current()->GetClassLinker()->GetOatMethodQuickCodeFor(this) != nullptr;
503}
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000504
Andreas Gampe542451c2016-07-26 09:02:02 -0700505void ArtMethod::CopyFrom(ArtMethod* src, PointerSize image_pointer_size) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000506 memcpy(reinterpret_cast<void*>(this), reinterpret_cast<const void*>(src),
507 Size(image_pointer_size));
508 declaring_class_ = GcRoot<mirror::Class>(const_cast<ArtMethod*>(src)->GetDeclaringClass());
509
510 // If the entry point of the method we are copying from is from JIT code, we just
511 // put the entry point of the new method to interpreter. We could set the entry point
512 // to the JIT code, but this would require taking the JIT code cache lock to notify
513 // it, which we do not want at this level.
514 Runtime* runtime = Runtime::Current();
Calin Juravleffc87072016-04-20 14:22:09 +0100515 if (runtime->UseJitCompilation()) {
Nicolas Geoffray22cf3d32015-11-02 11:57:11 +0000516 if (runtime->GetJit()->GetCodeCache()->ContainsPc(GetEntryPointFromQuickCompiledCode())) {
517 SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), image_pointer_size);
518 }
519 }
520 // Clear the profiling info for the same reasons as the JIT code.
521 if (!src->IsNative()) {
522 SetProfilingInfoPtrSize(nullptr, image_pointer_size);
523 }
524 // Clear hotness to let the JIT properly decide when to compile this method.
525 hotness_count_ = 0;
526}
527
Andreas Gampe542451c2016-07-26 09:02:02 -0700528bool ArtMethod::IsImagePointerSize(PointerSize pointer_size) {
Andreas Gampe479b1de2016-07-19 18:27:17 -0700529 // Hijack this function to get access to PtrSizedFieldsOffset.
530 //
531 // Ensure that PrtSizedFieldsOffset is correct. We rely here on usually having both 32-bit and
532 // 64-bit builds.
533 static_assert(std::is_standard_layout<ArtMethod>::value, "ArtMethod is not standard layout.");
Andreas Gampe542451c2016-07-26 09:02:02 -0700534 static_assert(
535 (sizeof(void*) != 4) ||
536 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k32)),
537 "Unexpected 32-bit class layout.");
538 static_assert(
539 (sizeof(void*) != 8) ||
540 (offsetof(ArtMethod, ptr_sized_fields_) == PtrSizedFieldsOffset(PointerSize::k64)),
541 "Unexpected 64-bit class layout.");
Andreas Gampe479b1de2016-07-19 18:27:17 -0700542
Andreas Gampe75f08852016-07-19 08:06:07 -0700543 Runtime* runtime = Runtime::Current();
544 if (runtime == nullptr) {
545 return true;
546 }
547 return runtime->GetClassLinker()->GetImagePointerSize() == pointer_size;
548}
549
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800550} // namespace art