blob: 6af4cdb2cbf446bd83f47c9cffc91f044171700c [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 Rogers62f05122014-03-21 11:21:29 -070019#include "art_field-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070020#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "base/stringpiece.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070022#include "class-inl.h"
23#include "dex_file-inl.h"
Ian Rogersc449aa82013-07-29 14:35:46 -070024#include "dex_instruction.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070025#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "interpreter/interpreter.h"
27#include "jni_internal.h"
Ian Rogers1809a722013-08-09 22:05:32 -070028#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "object-inl.h"
30#include "object_array.h"
31#include "object_array-inl.h"
Ian Rogers62f05122014-03-21 11:21:29 -070032#include "scoped_thread_state_change.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "string.h"
34#include "object_utils.h"
Ian Rogers62f05122014-03-21 11:21:29 -070035#include "well_known_classes.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036
37namespace art {
38namespace mirror {
39
Brian Carlstromea46f952013-07-30 01:26:50 -070040extern "C" void art_portable_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*, char);
Ian Rogers0177e532014-02-11 16:30:46 -080041extern "C" void art_quick_invoke_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
42 const char*);
Stuart Monteithb95a5342014-03-12 13:32:32 +000043#ifdef __LP64__
Ian Rogers936b37f2014-02-14 00:52:24 -080044extern "C" void art_quick_invoke_static_stub(ArtMethod*, uint32_t*, uint32_t, Thread*, JValue*,
45 const char*);
46#endif
Jeff Hao5d917302013-02-27 17:57:33 -080047
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048// TODO: get global references for these
Brian Carlstromea46f952013-07-30 01:26:50 -070049Class* ArtMethod::java_lang_reflect_ArtMethod_ = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070051ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccessAlreadyRunnable& soa,
52 jobject jlr_method) {
Ian Rogers62f05122014-03-21 11:21:29 -070053 mirror::ArtField* f =
54 soa.DecodeField(WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
55 mirror::ArtMethod* method = f->GetObject(soa.Decode<mirror::Object*>(jlr_method))->AsArtMethod();
56 DCHECK(method != nullptr);
57 return method;
58}
59
60
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080061void ArtMethod::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080062 if (java_lang_reflect_ArtMethod_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -080063 callback(reinterpret_cast<mirror::Object**>(&java_lang_reflect_ArtMethod_), arg, 0,
64 kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080065 }
66}
67
Ian Rogersef7d42f2014-01-06 12:55:46 -080068InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 // TODO: kSuper?
70 if (GetDeclaringClass()->IsInterface()) {
71 return kInterface;
72 } else if (IsStatic()) {
73 return kStatic;
74 } else if (IsDirect()) {
75 return kDirect;
76 } else {
77 return kVirtual;
78 }
79}
80
Brian Carlstromea46f952013-07-30 01:26:50 -070081void ArtMethod::SetClass(Class* java_lang_reflect_ArtMethod) {
82 CHECK(java_lang_reflect_ArtMethod_ == NULL);
83 CHECK(java_lang_reflect_ArtMethod != NULL);
84 java_lang_reflect_ArtMethod_ = java_lang_reflect_ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085}
86
Brian Carlstromea46f952013-07-30 01:26:50 -070087void ArtMethod::ResetClass() {
88 CHECK(java_lang_reflect_ArtMethod_ != NULL);
89 java_lang_reflect_ArtMethod_ = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090}
91
Brian Carlstromea46f952013-07-30 01:26:50 -070092void ArtMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010093 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_strings_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070094 new_dex_cache_strings);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095}
96
Brian Carlstromea46f952013-07-30 01:26:50 -070097void ArtMethod::SetDexCacheResolvedMethods(ObjectArray<ArtMethod>* new_dex_cache_methods) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010098 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_methods_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070099 new_dex_cache_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100}
101
Brian Carlstromea46f952013-07-30 01:26:50 -0700102void ArtMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100103 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_types_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700104 new_dex_cache_classes);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105}
106
Brian Carlstromea46f952013-07-30 01:26:50 -0700107size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 CHECK_LE(1, shorty.length());
109 uint32_t num_registers = 0;
110 for (int i = 1; i < shorty.length(); ++i) {
111 char ch = shorty[i];
112 if (ch == 'D' || ch == 'J') {
113 num_registers += 2;
114 } else {
115 num_registers += 1;
116 }
117 }
118 return num_registers;
119}
120
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121bool ArtMethod::IsProxyMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 return GetDeclaringClass()->IsProxyClass();
123}
124
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125ArtMethod* ArtMethod::FindOverriddenMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 if (IsStatic()) {
127 return NULL;
128 }
129 Class* declaring_class = GetDeclaringClass();
130 Class* super_class = declaring_class->GetSuperClass();
131 uint16_t method_index = GetMethodIndex();
Brian Carlstromea46f952013-07-30 01:26:50 -0700132 ObjectArray<ArtMethod>* super_class_vtable = super_class->GetVTable();
133 ArtMethod* result = NULL;
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
136 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
137 result = super_class_vtable->Get(method_index);
138 } else {
139 // Method didn't override superclass method so search interfaces
140 if (IsProxyMethod()) {
141 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
142 CHECK_EQ(result,
143 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
144 } else {
145 MethodHelper mh(this);
146 MethodHelper interface_mh;
147 IfTable* iftable = GetDeclaringClass()->GetIfTable();
148 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
149 Class* interface = iftable->GetInterface(i);
150 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700151 ArtMethod* interface_method = interface->GetVirtualMethod(j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 interface_mh.ChangeMethod(interface_method);
153 if (mh.HasSameNameAndSignature(&interface_mh)) {
154 result = interface_method;
155 break;
156 }
157 }
158 }
159 }
160 }
161#ifndef NDEBUG
162 MethodHelper result_mh(result);
163 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
164#endif
165 return result;
166}
167
Ian Rogersef7d42f2014-01-06 12:55:46 -0800168uintptr_t ArtMethod::NativePcOffset(const uintptr_t pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800169 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
170 return pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171}
172
Dave Allisonb373e092014-02-20 16:06:36 -0800173uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800174 if (IsPortableCompiled()) {
175 // Portable doesn't use the machine pc, we just use dex pc instead.
176 return static_cast<uint32_t>(pc);
177 }
Ian Rogers1809a722013-08-09 22:05:32 -0700178 MappingTable table(GetMappingTable());
179 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
181 return DexFile::kDexNoIndex; // Special no mapping case
182 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800183 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
184 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers1809a722013-08-09 22:05:32 -0700185 // Assume the caller wants a pc-to-dex mapping so check here first.
186 typedef MappingTable::PcToDexIterator It;
187 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
188 if (cur.NativePcOffset() == sought_offset) {
189 return cur.DexPc();
190 }
191 }
192 // Now check dex-to-pc mappings.
193 typedef MappingTable::DexToPcIterator It2;
194 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
195 if (cur.NativePcOffset() == sought_offset) {
196 return cur.DexPc();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 }
198 }
Dave Allisonb373e092014-02-20 16:06:36 -0800199 if (abort_on_failure) {
200 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
201 << "(PC " << reinterpret_cast<void*>(pc) << ", code=" << code
202 << ") in " << PrettyMethod(this);
203 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204 return DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205}
206
Ian Rogersef7d42f2014-01-06 12:55:46 -0800207uintptr_t ArtMethod::ToNativePc(const uint32_t dex_pc) {
Ian Rogers1809a722013-08-09 22:05:32 -0700208 MappingTable table(GetMappingTable());
209 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 DCHECK_EQ(dex_pc, 0U);
211 return 0; // Special no mapping/pc == 0 case
212 }
Ian Rogers1809a722013-08-09 22:05:32 -0700213 // Assume the caller wants a dex-to-pc mapping so check here first.
214 typedef MappingTable::DexToPcIterator It;
215 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
216 if (cur.DexPc() == dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800217 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
Ian Rogers1809a722013-08-09 22:05:32 -0700218 return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 }
220 }
Ian Rogers1809a722013-08-09 22:05:32 -0700221 // Now check pc-to-dex mappings.
222 typedef MappingTable::PcToDexIterator It2;
223 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
224 if (cur.DexPc() == dex_pc) {
225 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
226 return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset();
227 }
228 }
229 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 << " in " << PrettyMethod(this);
231 return 0;
232}
233
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700234uint32_t ArtMethod::FindCatchBlock(Handle<Class>& exception_type, uint32_t dex_pc,
Andreas Gampe72b3e432014-05-13 21:42:05 -0700235 bool* has_no_move_exception, bool* exc_changed) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 MethodHelper mh(this);
237 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700238 // Set aside the exception while we resolve its type.
239 Thread* self = Thread::Current();
240 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700241 StackHandleScope<1> hs(self);
242 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
Jeff Haoaa961912014-04-22 13:54:32 -0700243 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700244 // Default to handler not found.
245 uint32_t found_dex_pc = DexFile::kDexNoIndex;
246 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
248 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
249 // Catch all case
250 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700251 found_dex_pc = it.GetHandlerAddress();
252 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253 }
254 // Does this catch exception type apply?
Jeff Haoaa961912014-04-22 13:54:32 -0700255 Class* iter_exception_type = mh.GetClassFromTypeIdx(iter_type_idx);
Andreas Gampe72b3e432014-05-13 21:42:05 -0700256 if (iter_exception_type == nullptr) {
257 // Now have a NoClassDefFoundError as exception.
258 // Note: this is not RI behavior. RI would have failed when loading the class.
259 *exc_changed = true;
260
261 // TODO: Add old exception as suppressed.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700263 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Andreas Gampe72b3e432014-05-13 21:42:05 -0700264
265 // Return immediately.
266 return DexFile::kDexNoIndex;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700267 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700268 found_dex_pc = it.GetHandlerAddress();
269 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 }
271 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700272 if (found_dex_pc != DexFile::kDexNoIndex) {
273 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700274 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700275 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
276 }
Jeff Haoaa961912014-04-22 13:54:32 -0700277 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700278 if (exception.Get() != nullptr) {
279 self->SetException(throw_location, exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700280 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700281 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282}
283
Brian Carlstromea46f952013-07-30 01:26:50 -0700284void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800285 const char* shorty) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 if (kIsDebugBuild) {
287 self->AssertThreadSuspensionIsAllowable();
288 CHECK_EQ(kRunnable, self->GetState());
Ian Rogers0177e532014-02-11 16:30:46 -0800289 CHECK_STREQ(MethodHelper(this).GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290 }
291
292 // Push a transition back into managed code onto the linked list in thread.
293 ManagedStack fragment;
294 self->PushManagedStackFragment(&fragment);
295
Ian Rogers62d6c772013-02-27 08:32:07 -0800296 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700297 // Call the invoke stub, passing everything as arguments.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700298 if (UNLIKELY(!runtime->IsStarted())) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700299 if (IsStatic()) {
300 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result);
301 } else {
302 Object* receiver = reinterpret_cast<StackReference<Object>*>(&args[0])->AsMirrorPtr();
303 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304 }
305 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 const bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800307 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
308 bool have_portable_code = GetEntryPointFromPortableCompiledCode() != nullptr;
309 if (LIKELY(have_quick_code || have_portable_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700310 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800311 LOG(INFO) << StringPrintf("Invoking '%s' %s code=%p", PrettyMethod(this).c_str(),
312 have_quick_code ? "quick" : "portable",
313 have_quick_code ? GetEntryPointFromQuickCompiledCode()
314 : GetEntryPointFromPortableCompiledCode());
Jeff Hao790ad902013-05-22 15:02:08 -0700315 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800316 if (!IsPortableCompiled()) {
Stuart Monteithb95a5342014-03-12 13:32:32 +0000317#ifdef __LP64__
Ian Rogers936b37f2014-02-14 00:52:24 -0800318 if (!IsStatic()) {
319 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
320 } else {
321 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
322 }
323#else
Ian Rogers0177e532014-02-11 16:30:46 -0800324 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogers936b37f2014-02-14 00:52:24 -0800325#endif
Ian Rogersef7d42f2014-01-06 12:55:46 -0800326 } else {
Ian Rogers0177e532014-02-11 16:30:46 -0800327 (*art_portable_invoke_stub)(this, args, args_size, self, result, shorty[0]);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800328 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200329 if (UNLIKELY(self->GetException(nullptr) == Thread::GetDeoptimizationException())) {
330 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700331 // exception was thrown to force the activations to be removed from the
332 // stack. Continue execution in the interpreter.
333 self->ClearException();
334 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200335 self->SetTopOfStack(nullptr, 0);
Jeff Hao790ad902013-05-22 15:02:08 -0700336 self->SetTopOfShadowStack(shadow_frame);
337 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
338 }
339 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800340 LOG(INFO) << StringPrintf("Returned '%s' %s code=%p", PrettyMethod(this).c_str(),
341 have_quick_code ? "quick" : "portable",
342 have_quick_code ? GetEntryPointFromQuickCompiledCode()
343 : GetEntryPointFromPortableCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800344 }
345 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800346 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Jeff Hao5d917302013-02-27 17:57:33 -0800347 if (result != NULL) {
348 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 }
350 }
351 }
352
353 // Pop transition.
354 self->PopManagedStackFragment(fragment);
355}
356
Ian Rogersef7d42f2014-01-06 12:55:46 -0800357bool ArtMethod::IsRegistered() {
358 void* native_method =
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700359 GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, entry_point_from_jni_));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800360 CHECK(native_method != nullptr);
Jeff Hao79fe5392013-04-24 18:41:58 -0700361 void* jni_stub = GetJniDlsymLookupStub();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362 return native_method != jni_stub;
363}
364
Ian Rogers1eb512d2013-10-18 15:42:20 -0700365void ArtMethod::RegisterNative(Thread* self, const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 DCHECK(Thread::Current() == self);
367 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700368 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 CHECK(native_method != NULL) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700370 if (is_fast) {
371 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 }
Ian Rogers987560f2014-04-22 11:42:59 -0700373 SetNativeMethod(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374}
375
Brian Carlstromea46f952013-07-30 01:26:50 -0700376void ArtMethod::UnregisterNative(Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700377 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378 // restore stub to lookup native pointer via dlsym
Ian Rogers1eb512d2013-10-18 15:42:20 -0700379 RegisterNative(self, GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380}
381
Vladimir Marko8a630572014-04-09 18:45:35 +0100382const void* ArtMethod::GetOatCodePointer() {
383 if (IsPortableCompiled() || IsNative() || IsAbstract() || IsRuntimeMethod() || IsProxyMethod()) {
384 return nullptr;
385 }
386 Runtime* runtime = Runtime::Current();
387 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this);
388 // On failure, instead of nullptr we get the quick-to-interpreter-bridge (but not the trampoline).
389 DCHECK(entry_point != GetQuickToInterpreterBridgeTrampoline(runtime->GetClassLinker()));
390 if (entry_point == GetQuickToInterpreterBridge()) {
391 return nullptr;
392 }
393 return EntryPointToCodePointer(entry_point);
394}
395
396const uint8_t* ArtMethod::GetMappingTable() {
397 const void* code = GetOatCodePointer();
398 if (code == nullptr) {
399 return nullptr;
400 }
Vladimir Marko7624d252014-05-02 14:40:15 +0100401 uint32_t offset = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].mapping_table_offset_;
Vladimir Marko8a630572014-04-09 18:45:35 +0100402 if (UNLIKELY(offset == 0u)) {
403 return nullptr;
404 }
405 return reinterpret_cast<const uint8_t*>(code) - offset;
406}
407
408const uint8_t* ArtMethod::GetVmapTable() {
409 const void* code = GetOatCodePointer();
410 if (code == nullptr) {
411 return nullptr;
412 }
Vladimir Marko7624d252014-05-02 14:40:15 +0100413 uint32_t offset = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].vmap_table_offset_;
Vladimir Marko8a630572014-04-09 18:45:35 +0100414 if (UNLIKELY(offset == 0u)) {
415 return nullptr;
416 }
417 return reinterpret_cast<const uint8_t*>(code) - offset;
418}
419
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800420} // namespace mirror
421} // namespace art