blob: 0632a68afafedbf1a01bc32649991c0c4b0510fa [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
Ian Rogers62f05122014-03-21 11:21:29 -070051ArtMethod* ArtMethod::FromReflectedMethod(const ScopedObjectAccess& soa, jobject jlr_method) {
52 mirror::ArtField* f =
53 soa.DecodeField(WellKnownClasses::java_lang_reflect_AbstractMethod_artMethod);
54 mirror::ArtMethod* method = f->GetObject(soa.Decode<mirror::Object*>(jlr_method))->AsArtMethod();
55 DCHECK(method != nullptr);
56 return method;
57}
58
59
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080060void ArtMethod::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080061 if (java_lang_reflect_ArtMethod_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -080062 callback(reinterpret_cast<mirror::Object**>(&java_lang_reflect_ArtMethod_), arg, 0,
63 kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080064 }
65}
66
Ian Rogersef7d42f2014-01-06 12:55:46 -080067InvokeType ArtMethod::GetInvokeType() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 // TODO: kSuper?
69 if (GetDeclaringClass()->IsInterface()) {
70 return kInterface;
71 } else if (IsStatic()) {
72 return kStatic;
73 } else if (IsDirect()) {
74 return kDirect;
75 } else {
76 return kVirtual;
77 }
78}
79
Brian Carlstromea46f952013-07-30 01:26:50 -070080void ArtMethod::SetClass(Class* java_lang_reflect_ArtMethod) {
81 CHECK(java_lang_reflect_ArtMethod_ == NULL);
82 CHECK(java_lang_reflect_ArtMethod != NULL);
83 java_lang_reflect_ArtMethod_ = java_lang_reflect_ArtMethod;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084}
85
Brian Carlstromea46f952013-07-30 01:26:50 -070086void ArtMethod::ResetClass() {
87 CHECK(java_lang_reflect_ArtMethod_ != NULL);
88 java_lang_reflect_ArtMethod_ = NULL;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089}
90
Brian Carlstromea46f952013-07-30 01:26:50 -070091void ArtMethod::SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010092 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_strings_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070093 new_dex_cache_strings);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094}
95
Brian Carlstromea46f952013-07-30 01:26:50 -070096void ArtMethod::SetDexCacheResolvedMethods(ObjectArray<ArtMethod>* new_dex_cache_methods) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010097 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_methods_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070098 new_dex_cache_methods);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099}
100
Brian Carlstromea46f952013-07-30 01:26:50 -0700101void ArtMethod::SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_classes) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100102 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, dex_cache_resolved_types_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700103 new_dex_cache_classes);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104}
105
Brian Carlstromea46f952013-07-30 01:26:50 -0700106size_t ArtMethod::NumArgRegisters(const StringPiece& shorty) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 CHECK_LE(1, shorty.length());
108 uint32_t num_registers = 0;
109 for (int i = 1; i < shorty.length(); ++i) {
110 char ch = shorty[i];
111 if (ch == 'D' || ch == 'J') {
112 num_registers += 2;
113 } else {
114 num_registers += 1;
115 }
116 }
117 return num_registers;
118}
119
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120bool ArtMethod::IsProxyMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 return GetDeclaringClass()->IsProxyClass();
122}
123
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124ArtMethod* ArtMethod::FindOverriddenMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 if (IsStatic()) {
126 return NULL;
127 }
128 Class* declaring_class = GetDeclaringClass();
129 Class* super_class = declaring_class->GetSuperClass();
130 uint16_t method_index = GetMethodIndex();
Brian Carlstromea46f952013-07-30 01:26:50 -0700131 ObjectArray<ArtMethod>* super_class_vtable = super_class->GetVTable();
132 ArtMethod* result = NULL;
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
135 if (super_class_vtable != NULL && method_index < super_class_vtable->GetLength()) {
136 result = super_class_vtable->Get(method_index);
137 } else {
138 // Method didn't override superclass method so search interfaces
139 if (IsProxyMethod()) {
140 result = GetDexCacheResolvedMethods()->Get(GetDexMethodIndex());
141 CHECK_EQ(result,
142 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
143 } else {
144 MethodHelper mh(this);
145 MethodHelper interface_mh;
146 IfTable* iftable = GetDeclaringClass()->GetIfTable();
147 for (size_t i = 0; i < iftable->Count() && result == NULL; i++) {
148 Class* interface = iftable->GetInterface(i);
149 for (size_t j = 0; j < interface->NumVirtualMethods(); ++j) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700150 ArtMethod* interface_method = interface->GetVirtualMethod(j);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 interface_mh.ChangeMethod(interface_method);
152 if (mh.HasSameNameAndSignature(&interface_mh)) {
153 result = interface_method;
154 break;
155 }
156 }
157 }
158 }
159 }
160#ifndef NDEBUG
161 MethodHelper result_mh(result);
162 DCHECK(result == NULL || MethodHelper(this).HasSameNameAndSignature(&result_mh));
163#endif
164 return result;
165}
166
Ian Rogersef7d42f2014-01-06 12:55:46 -0800167uintptr_t ArtMethod::NativePcOffset(const uintptr_t pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
169 return pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170}
171
Dave Allisonb373e092014-02-20 16:06:36 -0800172uint32_t ArtMethod::ToDexPc(const uintptr_t pc, bool abort_on_failure) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800173 if (IsPortableCompiled()) {
174 // Portable doesn't use the machine pc, we just use dex pc instead.
175 return static_cast<uint32_t>(pc);
176 }
Ian Rogers1809a722013-08-09 22:05:32 -0700177 MappingTable table(GetMappingTable());
178 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 DCHECK(IsNative() || IsCalleeSaveMethod() || IsProxyMethod()) << PrettyMethod(this);
180 return DexFile::kDexNoIndex; // Special no mapping case
181 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800182 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
183 uint32_t sought_offset = pc - reinterpret_cast<uintptr_t>(code);
Ian Rogers1809a722013-08-09 22:05:32 -0700184 // Assume the caller wants a pc-to-dex mapping so check here first.
185 typedef MappingTable::PcToDexIterator It;
186 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
187 if (cur.NativePcOffset() == sought_offset) {
188 return cur.DexPc();
189 }
190 }
191 // Now check dex-to-pc mappings.
192 typedef MappingTable::DexToPcIterator It2;
193 for (It2 cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
194 if (cur.NativePcOffset() == sought_offset) {
195 return cur.DexPc();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 }
197 }
Dave Allisonb373e092014-02-20 16:06:36 -0800198 if (abort_on_failure) {
199 LOG(FATAL) << "Failed to find Dex offset for PC offset " << reinterpret_cast<void*>(sought_offset)
200 << "(PC " << reinterpret_cast<void*>(pc) << ", code=" << code
201 << ") in " << PrettyMethod(this);
202 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 return DexFile::kDexNoIndex;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204}
205
Ian Rogersef7d42f2014-01-06 12:55:46 -0800206uintptr_t ArtMethod::ToNativePc(const uint32_t dex_pc) {
Ian Rogers1809a722013-08-09 22:05:32 -0700207 MappingTable table(GetMappingTable());
208 if (table.TotalSize() == 0) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 DCHECK_EQ(dex_pc, 0U);
210 return 0; // Special no mapping/pc == 0 case
211 }
Ian Rogers1809a722013-08-09 22:05:32 -0700212 // Assume the caller wants a dex-to-pc mapping so check here first.
213 typedef MappingTable::DexToPcIterator It;
214 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
215 if (cur.DexPc() == dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800216 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
Ian Rogers1809a722013-08-09 22:05:32 -0700217 return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 }
219 }
Ian Rogers1809a722013-08-09 22:05:32 -0700220 // Now check pc-to-dex mappings.
221 typedef MappingTable::PcToDexIterator It2;
222 for (It2 cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
223 if (cur.DexPc() == dex_pc) {
224 const void* code = Runtime::Current()->GetInstrumentation()->GetQuickCodeFor(this);
225 return reinterpret_cast<uintptr_t>(code) + cur.NativePcOffset();
226 }
227 }
228 LOG(FATAL) << "Failed to find native offset for dex pc 0x" << std::hex << dex_pc
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 << " in " << PrettyMethod(this);
230 return 0;
231}
232
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700233uint32_t ArtMethod::FindCatchBlock(Handle<Class>& exception_type, uint32_t dex_pc,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800234 bool* has_no_move_exception) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 MethodHelper mh(this);
236 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Jeff Haoaa961912014-04-22 13:54:32 -0700237 // Set aside the exception while we resolve its type.
238 Thread* self = Thread::Current();
239 ThrowLocation throw_location;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700240 StackHandleScope<1> hs(self);
241 Handle<mirror::Throwable> exception(hs.NewHandle(self->GetException(&throw_location)));
Jeff Haoaa961912014-04-22 13:54:32 -0700242 self->ClearException();
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700243 // Default to handler not found.
244 uint32_t found_dex_pc = DexFile::kDexNoIndex;
245 // Iterate over the catch handlers associated with dex_pc.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 for (CatchHandlerIterator it(*code_item, dex_pc); it.HasNext(); it.Next()) {
247 uint16_t iter_type_idx = it.GetHandlerTypeIndex();
248 // Catch all case
249 if (iter_type_idx == DexFile::kDexNoIndex16) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700250 found_dex_pc = it.GetHandlerAddress();
251 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 }
253 // Does this catch exception type apply?
Jeff Haoaa961912014-04-22 13:54:32 -0700254 Class* iter_exception_type = mh.GetClassFromTypeIdx(iter_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700255 if (exception_type.Get() == nullptr) {
Jeff Haoaa961912014-04-22 13:54:32 -0700256 self->ClearException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 LOG(WARNING) << "Unresolved exception class when finding catch block: "
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700258 << mh.GetTypeDescriptorFromTypeIdx(iter_type_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700259 } else if (iter_exception_type->IsAssignableFrom(exception_type.Get())) {
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700260 found_dex_pc = it.GetHandlerAddress();
261 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 }
263 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700264 if (found_dex_pc != DexFile::kDexNoIndex) {
265 const Instruction* first_catch_instr =
Jeff Haoaa961912014-04-22 13:54:32 -0700266 Instruction::At(&code_item->insns_[found_dex_pc]);
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700267 *has_no_move_exception = (first_catch_instr->Opcode() != Instruction::MOVE_EXCEPTION);
268 }
Jeff Haoaa961912014-04-22 13:54:32 -0700269 // Put the exception back.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700270 if (exception.Get() != nullptr) {
271 self->SetException(throw_location, exception.Get());
Jeff Haoaa961912014-04-22 13:54:32 -0700272 }
Ian Rogers9e8f45e2013-07-31 10:58:53 -0700273 return found_dex_pc;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274}
275
Brian Carlstromea46f952013-07-30 01:26:50 -0700276void ArtMethod::Invoke(Thread* self, uint32_t* args, uint32_t args_size, JValue* result,
Ian Rogers0177e532014-02-11 16:30:46 -0800277 const char* shorty) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 if (kIsDebugBuild) {
279 self->AssertThreadSuspensionIsAllowable();
280 CHECK_EQ(kRunnable, self->GetState());
Ian Rogers0177e532014-02-11 16:30:46 -0800281 CHECK_STREQ(MethodHelper(this).GetShorty(), shorty);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 }
283
284 // Push a transition back into managed code onto the linked list in thread.
285 ManagedStack fragment;
286 self->PushManagedStackFragment(&fragment);
287
Ian Rogers62d6c772013-02-27 08:32:07 -0800288 Runtime* runtime = Runtime::Current();
Jeff Hao74180ca2013-03-27 15:29:11 -0700289 // Call the invoke stub, passing everything as arguments.
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700290 if (UNLIKELY(!runtime->IsStarted())) {
Ian Rogers5d27faf2014-05-02 17:17:18 -0700291 if (IsStatic()) {
292 art::interpreter::EnterInterpreterFromInvoke(self, this, nullptr, args, result);
293 } else {
294 Object* receiver = reinterpret_cast<StackReference<Object>*>(&args[0])->AsMirrorPtr();
295 art::interpreter::EnterInterpreterFromInvoke(self, this, receiver, args + 1, result);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296 }
297 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800298 const bool kLogInvocationStartAndReturn = false;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800299 bool have_quick_code = GetEntryPointFromQuickCompiledCode() != nullptr;
300 bool have_portable_code = GetEntryPointFromPortableCompiledCode() != nullptr;
301 if (LIKELY(have_quick_code || have_portable_code)) {
Jeff Hao790ad902013-05-22 15:02:08 -0700302 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800303 LOG(INFO) << StringPrintf("Invoking '%s' %s code=%p", PrettyMethod(this).c_str(),
304 have_quick_code ? "quick" : "portable",
305 have_quick_code ? GetEntryPointFromQuickCompiledCode()
306 : GetEntryPointFromPortableCompiledCode());
Jeff Hao790ad902013-05-22 15:02:08 -0700307 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800308 if (!IsPortableCompiled()) {
Stuart Monteithb95a5342014-03-12 13:32:32 +0000309#ifdef __LP64__
Ian Rogers936b37f2014-02-14 00:52:24 -0800310 if (!IsStatic()) {
311 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
312 } else {
313 (*art_quick_invoke_static_stub)(this, args, args_size, self, result, shorty);
314 }
315#else
Ian Rogers0177e532014-02-11 16:30:46 -0800316 (*art_quick_invoke_stub)(this, args, args_size, self, result, shorty);
Ian Rogers936b37f2014-02-14 00:52:24 -0800317#endif
Ian Rogersef7d42f2014-01-06 12:55:46 -0800318 } else {
Ian Rogers0177e532014-02-11 16:30:46 -0800319 (*art_portable_invoke_stub)(this, args, args_size, self, result, shorty[0]);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800320 }
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200321 if (UNLIKELY(self->GetException(nullptr) == Thread::GetDeoptimizationException())) {
322 // Unusual case where we were running generated code and an
Jeff Hao790ad902013-05-22 15:02:08 -0700323 // exception was thrown to force the activations to be removed from the
324 // stack. Continue execution in the interpreter.
325 self->ClearException();
326 ShadowFrame* shadow_frame = self->GetAndClearDeoptimizationShadowFrame(result);
Sebastien Hertzfd3077e2014-04-23 10:32:43 +0200327 self->SetTopOfStack(nullptr, 0);
Jeff Hao790ad902013-05-22 15:02:08 -0700328 self->SetTopOfShadowStack(shadow_frame);
329 interpreter::EnterInterpreterFromDeoptimize(self, shadow_frame, result);
330 }
331 if (kLogInvocationStartAndReturn) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800332 LOG(INFO) << StringPrintf("Returned '%s' %s code=%p", PrettyMethod(this).c_str(),
333 have_quick_code ? "quick" : "portable",
334 have_quick_code ? GetEntryPointFromQuickCompiledCode()
335 : GetEntryPointFromPortableCompiledCode());
Jeff Hao5d917302013-02-27 17:57:33 -0800336 }
337 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800338 LOG(INFO) << "Not invoking '" << PrettyMethod(this) << "' code=null";
Jeff Hao5d917302013-02-27 17:57:33 -0800339 if (result != NULL) {
340 result->SetJ(0);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341 }
342 }
343 }
344
345 // Pop transition.
346 self->PopManagedStackFragment(fragment);
347}
348
Ian Rogersef7d42f2014-01-06 12:55:46 -0800349bool ArtMethod::IsRegistered() {
350 void* native_method =
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700351 GetFieldPtr<void*>(OFFSET_OF_OBJECT_MEMBER(ArtMethod, entry_point_from_jni_));
Ian Rogersef7d42f2014-01-06 12:55:46 -0800352 CHECK(native_method != nullptr);
Jeff Hao79fe5392013-04-24 18:41:58 -0700353 void* jni_stub = GetJniDlsymLookupStub();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 return native_method != jni_stub;
355}
356
Ian Rogers1eb512d2013-10-18 15:42:20 -0700357void ArtMethod::RegisterNative(Thread* self, const void* native_method, bool is_fast) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800358 DCHECK(Thread::Current() == self);
359 CHECK(IsNative()) << PrettyMethod(this);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700360 CHECK(!IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 CHECK(native_method != NULL) << PrettyMethod(this);
Ian Rogers987560f2014-04-22 11:42:59 -0700362 if (is_fast) {
363 SetAccessFlags(GetAccessFlags() | kAccFastNative);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800364 }
Ian Rogers987560f2014-04-22 11:42:59 -0700365 SetNativeMethod(native_method);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366}
367
Brian Carlstromea46f952013-07-30 01:26:50 -0700368void ArtMethod::UnregisterNative(Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700369 CHECK(IsNative() && !IsFastNative()) << PrettyMethod(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 // restore stub to lookup native pointer via dlsym
Ian Rogers1eb512d2013-10-18 15:42:20 -0700371 RegisterNative(self, GetJniDlsymLookupStub(), false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372}
373
Vladimir Marko8a630572014-04-09 18:45:35 +0100374const void* ArtMethod::GetOatCodePointer() {
375 if (IsPortableCompiled() || IsNative() || IsAbstract() || IsRuntimeMethod() || IsProxyMethod()) {
376 return nullptr;
377 }
378 Runtime* runtime = Runtime::Current();
379 const void* entry_point = runtime->GetInstrumentation()->GetQuickCodeFor(this);
380 // On failure, instead of nullptr we get the quick-to-interpreter-bridge (but not the trampoline).
381 DCHECK(entry_point != GetQuickToInterpreterBridgeTrampoline(runtime->GetClassLinker()));
382 if (entry_point == GetQuickToInterpreterBridge()) {
383 return nullptr;
384 }
385 return EntryPointToCodePointer(entry_point);
386}
387
388const uint8_t* ArtMethod::GetMappingTable() {
389 const void* code = GetOatCodePointer();
390 if (code == nullptr) {
391 return nullptr;
392 }
Vladimir Marko7624d252014-05-02 14:40:15 +0100393 uint32_t offset = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].mapping_table_offset_;
Vladimir Marko8a630572014-04-09 18:45:35 +0100394 if (UNLIKELY(offset == 0u)) {
395 return nullptr;
396 }
397 return reinterpret_cast<const uint8_t*>(code) - offset;
398}
399
400const uint8_t* ArtMethod::GetVmapTable() {
401 const void* code = GetOatCodePointer();
402 if (code == nullptr) {
403 return nullptr;
404 }
Vladimir Marko7624d252014-05-02 14:40:15 +0100405 uint32_t offset = reinterpret_cast<const OatQuickMethodHeader*>(code)[-1].vmap_table_offset_;
Vladimir Marko8a630572014-04-09 18:45:35 +0100406 if (UNLIKELY(offset == 0u)) {
407 return nullptr;
408 }
409 return reinterpret_cast<const uint8_t*>(code) - offset;
410}
411
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412} // namespace mirror
413} // namespace art