blob: b3170fa7b5d6c589e8aaaed1b7fef92047372629 [file] [log] [blame]
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001/*
2 * Copyright (C) 2012 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
17#ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
18#define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_
19
20#include "entrypoint_utils.h"
21
Nicolas Geoffrayd4ceecc2016-06-29 08:39:47 +000022#include "art_method.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070023#include "class_linker-inl.h"
24#include "common_throws.h"
25#include "dex_file.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010026#include "entrypoints/quick/callee_save_frame.h"
27#include "handle_scope-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "indirect_reference_table.h"
29#include "invoke_type.h"
30#include "jni_internal.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "mirror/array.h"
32#include "mirror/class-inl.h"
33#include "mirror/object-inl.h"
34#include "mirror/throwable.h"
Vladimir Marko5ea536a2015-04-20 20:11:30 +010035#include "nth_caller_visitor.h"
36#include "runtime.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010037#include "stack_map.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070038#include "thread.h"
39
40namespace art {
41
Mathieu Chartiere401d142015-04-22 13:56:20 -070042inline ArtMethod* GetResolvedMethod(ArtMethod* outer_method,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010043 const InlineInfo& inline_info,
David Srbecky61b28a12016-02-25 21:55:03 +000044 const InlineInfoEncoding& encoding,
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010045 uint8_t inlining_depth)
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010046 SHARED_REQUIRES(Locks::mutator_lock_) {
47 // This method is being used by artQuickResolutionTrampoline, before it sets up
48 // the passed parameters in a GC friendly way. Therefore we must never be
49 // suspended while executing it.
50 ScopedAssertNoThreadSuspension sants(Thread::Current(), __FUNCTION__);
51
David Srbecky61b28a12016-02-25 21:55:03 +000052 uint32_t method_index = inline_info.GetMethodIndexAtDepth(encoding, inlining_depth);
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010053 InvokeType invoke_type = static_cast<InvokeType>(
David Srbecky61b28a12016-02-25 21:55:03 +000054 inline_info.GetInvokeTypeAtDepth(encoding, inlining_depth));
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010055 ArtMethod* inlined_method = outer_method->GetDexCacheResolvedMethod(method_index, sizeof(void*));
56 if (!inlined_method->IsRuntimeMethod()) {
57 return inlined_method;
Mathieu Chartier45bf2502016-03-31 11:07:09 -070058 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010059
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010060 // The method in the dex cache is the runtime method responsible for invoking
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +010061 // the stub that will then update the dex cache. Therefore, we need to do the
62 // resolution ourselves.
Nicolas Geoffray3976e5e2015-06-15 08:58:03 +010063
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010064 // We first find the dex cache of our caller. If it is the outer method, we can directly
65 // use its dex cache. Otherwise, we also need to resolve our caller.
66 ArtMethod* caller = outer_method;
67 if (inlining_depth != 0) {
68 caller = GetResolvedMethod(outer_method,
69 inline_info,
70 encoding,
71 inlining_depth - 1);
72 }
73 DCHECK_EQ(caller->GetDexCache(), outer_method->GetDexCache())
74 << "Compiler only supports inlining calls within the same dex cache";
75 const DexFile* dex_file = outer_method->GetDexFile();
76 const DexFile::MethodId& method_id = dex_file->GetMethodId(method_index);
77
78 if (inline_info.GetDexPcAtDepth(encoding, inlining_depth) == static_cast<uint32_t>(-1)) {
79 // "charAt" special case. It is the only non-leaf method we inline across dex files.
80 if (kIsDebugBuild) {
81 const char* name = dex_file->StringDataByIdx(method_id.name_idx_);
82 DCHECK_EQ(std::string(name), "charAt");
83 DCHECK_EQ(std::string(dex_file->GetMethodShorty(method_id)), "CI")
84 << std::string(dex_file->GetMethodShorty(method_id));
85 DCHECK_EQ(std::string(dex_file->StringByTypeIdx(method_id.class_idx_)), "Ljava/lang/String;")
86 << std::string(dex_file->StringByTypeIdx(method_id.class_idx_));
87 }
88 mirror::Class* cls =
89 Runtime::Current()->GetClassLinker()->GetClassRoot(ClassLinker::kJavaLangString);
90 // Update the dex cache for future lookups.
91 caller->GetDexCache()->SetResolvedType(method_id.class_idx_, cls);
92 inlined_method = cls->FindVirtualMethod("charAt", "(I)C", sizeof(void*));
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +010093 } else {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +010094 mirror::Class* klass = caller->GetDexCache()->GetResolvedType(method_id.class_idx_);
95 DCHECK_EQ(klass->GetDexCache(), caller->GetDexCache())
96 << "Compiler only supports inlining calls within the same dex cache";
97 switch (invoke_type) {
98 case kDirect:
99 case kStatic:
100 inlined_method =
101 klass->FindDirectMethod(klass->GetDexCache(), method_index, sizeof(void*));
102 break;
103 case kSuper:
104 case kVirtual:
105 inlined_method =
106 klass->FindVirtualMethod(klass->GetDexCache(), method_index, sizeof(void*));
107 break;
108 default:
109 LOG(FATAL) << "Unimplemented inlined invocation type: " << invoke_type;
110 UNREACHABLE();
111 }
Nicolas Geoffray32c9ea52015-06-12 14:52:33 +0100112 }
113
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100114 // Update the dex cache for future lookups. Note that for static methods, this is safe
115 // when the class is being initialized, as the entrypoint for the ArtMethod is at
116 // this point still the resolution trampoline.
117 outer_method->SetDexCacheResolvedMethod(method_index, inlined_method, sizeof(void*));
118 return inlined_method;
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100119}
120
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121inline ArtMethod* GetCalleeSaveMethodCaller(Thread* self, Runtime::CalleeSaveType type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700122 SHARED_REQUIRES(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100123 return GetCalleeSaveMethodCaller(
124 self->GetManagedStack()->GetTopQuickFrame(), type, true /* do_caller_check */);
125}
126
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700127template <const bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700128ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800129inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800131 Thread* self, bool* slow_path) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100132 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
133 size_t pointer_size = class_linker->GetImagePointerSize();
134 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700135 if (UNLIKELY(klass == nullptr)) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100136 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700137 *slow_path = true;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700138 if (klass == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700139 DCHECK(self->IsExceptionPending());
140 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700141 } else {
142 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700143 }
144 }
145 if (kAccessCheck) {
146 if (UNLIKELY(!klass->IsInstantiable())) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000147 self->ThrowNewException("Ljava/lang/InstantiationError;", PrettyDescriptor(klass).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700148 *slow_path = true;
149 return nullptr; // Failure
150 }
151 mirror::Class* referrer = method->GetDeclaringClass();
152 if (UNLIKELY(!referrer->CanAccess(klass))) {
153 ThrowIllegalAccessErrorClass(referrer, klass);
154 *slow_path = true;
155 return nullptr; // Failure
156 }
157 }
158 if (UNLIKELY(!klass->IsInitialized())) {
159 StackHandleScope<1> hs(self);
160 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
161 // EnsureInitialized (the class initializer) might cause a GC.
162 // may cause us to suspend meaning that another thread may try to
163 // change the allocator while we are stuck in the entrypoints of
164 // an old allocator. Also, the class initialization may fail. To
165 // handle these cases we mark the slow path boolean as true so
166 // that the caller knows to check the allocator type to see if it
167 // has changed and to null-check the return value in case the
168 // initialization fails.
169 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700170 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700171 DCHECK(self->IsExceptionPending());
172 return nullptr; // Failure
Mathieu Chartier524507a2014-08-27 15:28:28 -0700173 } else {
174 DCHECK(!self->IsExceptionPending());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700175 }
176 return h_klass.Get();
177 }
178 return klass;
179}
180
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700181ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800182inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
183 Thread* self,
184 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700185 if (UNLIKELY(!klass->IsInitialized())) {
186 StackHandleScope<1> hs(self);
187 Handle<mirror::Class> h_class(hs.NewHandle(klass));
188 // EnsureInitialized (the class initializer) might cause a GC.
189 // may cause us to suspend meaning that another thread may try to
190 // change the allocator while we are stuck in the entrypoints of
191 // an old allocator. Also, the class initialization may fail. To
192 // handle these cases we mark the slow path boolean as true so
193 // that the caller knows to check the allocator type to see if it
194 // has changed and to null-check the return value in case the
195 // initialization fails.
196 *slow_path = true;
Ian Rogers7b078e82014-09-10 14:44:24 -0700197 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700198 DCHECK(self->IsExceptionPending());
199 return nullptr; // Failure
200 }
201 return h_class.Get();
202 }
203 return klass;
204}
205
206// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
207// cannot be resolved, throw an error. If it can, use it to create an instance.
208// When verification/compiler hasn't been able to verify access, optionally perform an access
209// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700210template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700211ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800212inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800214 Thread* self,
215 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700216 bool slow_path = false;
217 mirror::Class* klass = CheckObjectAlloc<kAccessCheck>(type_idx, method, self, &slow_path);
218 if (UNLIKELY(slow_path)) {
219 if (klass == nullptr) {
220 return nullptr;
221 }
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800222 // CheckObjectAlloc can cause thread suspension which means we may now be instrumented.
223 return klass->Alloc</*kInstrumented*/true>(
224 self,
225 Runtime::Current()->GetHeap()->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700226 }
227 DCHECK(klass != nullptr);
228 return klass->Alloc<kInstrumented>(self, allocator_type);
229}
230
231// Given the context of a calling Method and a resolved class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700232template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700233ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800234inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
235 Thread* self,
236 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700237 DCHECK(klass != nullptr);
238 bool slow_path = false;
239 klass = CheckClassInitializedForObjectAlloc(klass, self, &slow_path);
240 if (UNLIKELY(slow_path)) {
241 if (klass == nullptr) {
242 return nullptr;
243 }
244 gc::Heap* heap = Runtime::Current()->GetHeap();
Roland Levillain91d65e02016-01-19 15:59:16 +0000245 // Pass in false since the object cannot be finalizable.
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800246 // CheckClassInitializedForObjectAlloc can cause thread suspension which means we may now be
247 // instrumented.
248 return klass->Alloc</*kInstrumented*/true, false>(self, heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700249 }
Roland Levillain91d65e02016-01-19 15:59:16 +0000250 // Pass in false since the object cannot be finalizable.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700251 return klass->Alloc<kInstrumented, false>(self, allocator_type);
252}
253
254// Given the context of a calling Method and an initialized class, create an instance.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700255template <bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700256ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800257inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
258 Thread* self,
259 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700260 DCHECK(klass != nullptr);
Roland Levillain91d65e02016-01-19 15:59:16 +0000261 // Pass in false since the object cannot be finalizable.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700262 return klass->Alloc<kInstrumented, false>(self, allocator_type);
263}
264
265
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700266template <bool kAccessCheck>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700267ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800268inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800269 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700270 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800271 bool* slow_path) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700272 if (UNLIKELY(component_count < 0)) {
273 ThrowNegativeArraySizeException(component_count);
274 *slow_path = true;
275 return nullptr; // Failure
276 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100277 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
278 size_t pointer_size = class_linker->GetImagePointerSize();
279 mirror::Class* klass = method->GetDexCacheResolvedType<false>(type_idx, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700280 if (UNLIKELY(klass == nullptr)) { // Not in dex cache so try to resolve
Vladimir Marko05792b92015-08-03 11:56:49 +0100281 klass = class_linker->ResolveType(type_idx, method);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700282 *slow_path = true;
283 if (klass == nullptr) { // Error
284 DCHECK(Thread::Current()->IsExceptionPending());
285 return nullptr; // Failure
286 }
287 CHECK(klass->IsArrayClass()) << PrettyClass(klass);
288 }
289 if (kAccessCheck) {
290 mirror::Class* referrer = method->GetDeclaringClass();
291 if (UNLIKELY(!referrer->CanAccess(klass))) {
292 ThrowIllegalAccessErrorClass(referrer, klass);
293 *slow_path = true;
294 return nullptr; // Failure
295 }
296 }
297 return klass;
298}
299
300// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
301// it cannot be resolved, throw an error. If it can, use it to create an array.
302// When verification/compiler hasn't been able to verify access, optionally perform an access
303// check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700304template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700305ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800306inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800307 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800309 Thread* self,
310 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700311 bool slow_path = false;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800312 mirror::Class* klass = CheckArrayAlloc<kAccessCheck>(type_idx, component_count, method,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700313 &slow_path);
314 if (UNLIKELY(slow_path)) {
315 if (klass == nullptr) {
316 return nullptr;
317 }
318 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier14b0a5d2016-03-11 17:22:23 -0800319 // CheckArrayAlloc can cause thread suspension which means we may now be instrumented.
320 return mirror::Array::Alloc</*kInstrumented*/true>(self,
321 klass,
322 component_count,
323 klass->GetComponentSizeShift(),
324 heap->GetCurrentAllocator());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700325 }
326 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700327 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700328}
329
330template <bool kAccessCheck, bool kInstrumented>
Hiroshi Yamauchieb1e9292014-08-06 12:41:15 -0700331ALWAYS_INLINE
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800332inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800333 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700334 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800335 Thread* self,
336 gc::AllocatorType allocator_type) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700337 DCHECK(klass != nullptr);
338 if (UNLIKELY(component_count < 0)) {
339 ThrowNegativeArraySizeException(component_count);
340 return nullptr; // Failure
341 }
342 if (kAccessCheck) {
343 mirror::Class* referrer = method->GetDeclaringClass();
344 if (UNLIKELY(!referrer->CanAccess(klass))) {
345 ThrowIllegalAccessErrorClass(referrer, klass);
346 return nullptr; // Failure
347 }
348 }
349 // No need to retry a slow-path allocation as the above code won't cause a GC or thread
350 // suspension.
351 return mirror::Array::Alloc<kInstrumented>(self, klass, component_count,
Hiroshi Yamauchif0edfc32014-09-25 11:46:46 -0700352 klass->GetComponentSizeShift(), allocator_type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700353}
354
355template<FindFieldType type, bool access_check>
Mathieu Chartierbf369182016-02-04 18:13:32 -0800356inline ArtField* FindFieldFromCode(uint32_t field_idx,
357 ArtMethod* referrer,
358 Thread* self,
359 size_t expected_size) REQUIRES(!Roles::uninterruptible_) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700360 bool is_primitive;
361 bool is_set;
362 bool is_static;
363 switch (type) {
364 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
365 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
366 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
367 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
368 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
369 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
370 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
371 case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through.
372 default: is_primitive = true; is_set = true; is_static = true; break;
373 }
374 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Igor Murashkinf1b4c412016-02-01 17:40:19 -0800375
376 ArtField* resolved_field;
377 if (access_check) {
378 // Slow path: According to JLS 13.4.8, a linkage error may occur if a compile-time
379 // qualifying type of a field and the resolved run-time qualifying type of a field differed
380 // in their static-ness.
381 //
382 // In particular, don't assume the dex instruction already correctly knows if the
383 // real field is static or not. The resolution must not be aware of this.
384 ArtMethod* method = referrer->GetInterfaceMethodIfProxy(sizeof(void*));
385
386 StackHandleScope<2> hs(self);
387 Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(method->GetDexCache()));
388 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(method->GetClassLoader()));
389
390 resolved_field = class_linker->ResolveFieldJLS(*method->GetDexFile(),
391 field_idx,
392 h_dex_cache,
393 h_class_loader);
394 } else {
395 // Fast path: Verifier already would've called ResolveFieldJLS and we wouldn't
396 // be executing here if there was a static/non-static mismatch.
397 resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
398 }
399
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700400 if (UNLIKELY(resolved_field == nullptr)) {
401 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
402 return nullptr; // Failure.
403 }
404 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
405 if (access_check) {
406 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
407 ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
408 return nullptr;
409 }
410 mirror::Class* referring_class = referrer->GetDeclaringClass();
411 if (UNLIKELY(!referring_class->CheckResolvedFieldAccess(fields_class, resolved_field,
412 field_idx))) {
413 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
414 return nullptr; // Failure.
415 }
416 if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
417 ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
418 return nullptr; // Failure.
419 } else {
420 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
421 resolved_field->FieldSize() != expected_size)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000422 self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;",
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700423 "Attempted read of %zd-bit %s on field '%s'",
424 expected_size * (32 / sizeof(int32_t)),
425 is_primitive ? "primitive" : "non-primitive",
426 PrettyField(resolved_field, true).c_str());
427 return nullptr; // Failure.
428 }
429 }
430 }
431 if (!is_static) {
432 // instance fields must be being accessed on an initialized class
433 return resolved_field;
434 } else {
435 // If the class is initialized we're done.
436 if (LIKELY(fields_class->IsInitialized())) {
437 return resolved_field;
438 } else {
439 StackHandleScope<1> hs(self);
440 Handle<mirror::Class> h_class(hs.NewHandle(fields_class));
Ian Rogers7b078e82014-09-10 14:44:24 -0700441 if (LIKELY(class_linker->EnsureInitialized(self, h_class, true, true))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700442 // Otherwise let's ensure the class is initialized before resolving the field.
443 return resolved_field;
444 }
445 DCHECK(self->IsExceptionPending()); // Throw exception and unwind
446 return nullptr; // Failure.
447 }
448 }
449}
450
451// Explicit template declarations of FindFieldFromCode for all field access types.
452#define EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700453template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartierc7853442015-03-27 14:35:38 -0700454ArtField* FindFieldFromCode<_type, _access_check>(uint32_t field_idx, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700455 ArtMethod* referrer, \
456 Thread* self, size_t expected_size) \
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700457
458#define EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
459 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, false); \
460 EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL(_type, true)
461
462EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectRead);
463EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstanceObjectWrite);
464EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveRead);
465EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(InstancePrimitiveWrite);
466EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectRead);
467EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticObjectWrite);
468EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveRead);
469EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL(StaticPrimitiveWrite);
470
471#undef EXPLICIT_FIND_FIELD_FROM_CODE_TYPED_TEMPLATE_DECL
472#undef EXPLICIT_FIND_FIELD_FROM_CODE_TEMPLATE_DECL
473
474template<InvokeType type, bool access_check>
Mathieu Chartiere401d142015-04-22 13:56:20 -0700475inline ArtMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object** this_object,
Andreas Gampe3a357142015-08-07 17:20:11 -0700476 ArtMethod* referrer, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700477 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Andreas Gampe3a357142015-08-07 17:20:11 -0700478 ArtMethod* resolved_method = class_linker->GetResolvedMethod(method_idx, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700479 if (resolved_method == nullptr) {
480 StackHandleScope<1> hs(self);
481 mirror::Object* null_this = nullptr;
482 HandleWrapper<mirror::Object> h_this(
483 hs.NewHandleWrapper(type == kStatic ? &null_this : this_object));
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800484 constexpr ClassLinker::ResolveMode resolve_mode =
485 access_check ? ClassLinker::kForceICCECheck
486 : ClassLinker::kNoICCECheckForCache;
487 resolved_method = class_linker->ResolveMethod<resolve_mode>(self, method_idx, referrer, type);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700488 }
489 if (UNLIKELY(resolved_method == nullptr)) {
490 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
491 return nullptr; // Failure.
492 } else if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
David Brazdil65902e82016-01-15 09:35:13 +0000493 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
494 resolved_method->IsConstructor())) {
495 // Hack for String init:
496 //
497 // We assume that the input of String.<init> in verified code is always
498 // an unitialized reference. If it is a null constant, it must have been
499 // optimized out by the compiler. Do not throw NullPointerException.
500 } else {
501 // Maintain interpreter-like semantics where NullPointerException is thrown
502 // after potential NoSuchMethodError from class linker.
503 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
504 return nullptr; // Failure.
505 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700506 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700507 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700508 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700509 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
510 resolved_method,
511 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700512 if (UNLIKELY(!can_access_resolved_method)) {
513 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
514 return nullptr; // Failure.
515 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100516 // Incompatible class change should have been handled in resolve method.
517 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
518 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
519 referrer);
520 return nullptr; // Failure.
521 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700522 }
523 switch (type) {
524 case kStatic:
525 case kDirect:
526 return resolved_method;
527 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700528 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700529 uint16_t vtable_index = resolved_method->GetMethodIndex();
530 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700531 (!klass->HasVTable() ||
532 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700533 // Behavior to agree with that of the verifier.
534 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
535 resolved_method->GetName(), resolved_method->GetSignature());
536 return nullptr; // Failure.
537 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700538 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700540 }
541 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700542 // TODO This lookup is quite slow.
543 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
544 // that will actually not be what we want in some cases where there are miranda methods or
545 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
546 // this method is coming from.
547 mirror::Class* referring_class = referrer->GetDeclaringClass();
548 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
549 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
550 if (UNLIKELY(method_reference_class == nullptr)) {
551 // Bad type idx.
552 CHECK(self->IsExceptionPending());
553 return nullptr;
554 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700555 // It is not an interface. If the referring class is in the class hierarchy of the
556 // referenced class in the bytecode, we use its super class. Otherwise, we throw
557 // a NoSuchMethodError.
558 mirror::Class* super_class = nullptr;
559 if (method_reference_class->IsAssignableFrom(referring_class)) {
560 super_class = referring_class->GetSuperClass();
561 }
Alex Light705ad492015-09-21 11:36:30 -0700562 uint16_t vtable_index = resolved_method->GetMethodIndex();
563 if (access_check) {
564 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700565 if (super_class == nullptr ||
566 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700567 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
568 // Behavior to agree with that of the verifier.
569 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
570 resolved_method->GetName(), resolved_method->GetSignature());
571 return nullptr; // Failure.
572 }
573 }
574 DCHECK(super_class != nullptr);
575 DCHECK(super_class->HasVTable());
576 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
577 } else {
578 // It is an interface.
579 if (access_check) {
580 if (!method_reference_class->IsAssignableFrom((*this_object)->GetClass())) {
581 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
582 method_reference_class,
583 *this_object,
584 referrer);
585 return nullptr; // Failure.
586 }
587 }
588 // TODO We can do better than this for a (compiled) fastpath.
589 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
590 resolved_method, class_linker->GetImagePointerSize());
591 // Throw an NSME if nullptr;
592 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700593 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
594 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700595 }
Alex Light705ad492015-09-21 11:36:30 -0700596 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700597 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700598 }
599 case kInterface: {
Nicolas Geoffray88f288e2016-06-29 08:17:52 +0000600 uint32_t imt_index = resolved_method->GetDexMethodIndex() % mirror::Class::kImtSize;
601 ArtMethod* imt_method = (*this_object)->GetClass()->GetEmbeddedImTableEntry(
602 imt_index, class_linker->GetImagePointerSize());
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000603 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700604 if (kIsDebugBuild) {
605 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700606 ArtMethod* method = klass->FindVirtualMethodForInterface(
607 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700608 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
609 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
610 PrettyClass(klass);
611 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700612 return imt_method;
613 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700614 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
615 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700616 if (UNLIKELY(interface_method == nullptr)) {
617 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700618 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700619 return nullptr; // Failure.
620 }
621 return interface_method;
622 }
623 }
624 default:
625 LOG(FATAL) << "Unknown invoke type " << type;
626 return nullptr; // Failure.
627 }
628}
629
630// Explicit template declarations of FindMethodFromCode for all invoke types.
631#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700632 template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
634 mirror::Object** this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700635 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700636 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700637#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
638 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
639 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
640
641EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
642EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
643EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
644EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
645EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
646
647#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
648#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
649
650// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700651inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
652 size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700653 ArtField* resolved_field =
654 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700655 if (UNLIKELY(resolved_field == nullptr)) {
656 return nullptr;
657 }
658 // Check for incompatible class change.
659 bool is_primitive;
660 bool is_set;
661 bool is_static;
662 switch (type) {
663 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
664 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
665 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
666 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
667 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
668 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
669 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
670 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
671 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700672 LOG(FATAL) << "UNREACHABLE";
673 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700674 }
675 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
676 // Incompatible class change.
677 return nullptr;
678 }
679 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
680 if (is_static) {
681 // Check class is initialized else fail so that we can contend to initialize the class with
682 // other threads that may be racing to do this.
683 if (UNLIKELY(!fields_class->IsInitialized())) {
684 return nullptr;
685 }
686 }
687 mirror::Class* referring_class = referrer->GetDeclaringClass();
688 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700689 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700690 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
691 // Illegal access.
692 return nullptr;
693 }
694 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
695 resolved_field->FieldSize() != expected_size)) {
696 return nullptr;
697 }
698 return resolved_field;
699}
700
701// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
703 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700704 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
705 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700706 }
Alex Light705ad492015-09-21 11:36:30 -0700707 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700708 ArtMethod* resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700709 referring_class->GetDexCache()->GetResolvedMethod(method_idx, sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700710 if (UNLIKELY(resolved_method == nullptr)) {
711 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700712 }
713 if (access_check) {
714 // Check for incompatible class change errors and access.
715 bool icce = resolved_method->CheckIncompatibleClassChange(type);
716 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700717 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700718 }
719 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700720 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
721 !referring_class->CanAccessMember(methods_class,
722 resolved_method->GetAccessFlags()))) {
723 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700724 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700725 }
726 }
727 if (type == kInterface) { // Most common form of slow path dispatch.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700728 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method, sizeof(void*));
Jeff Hao207a37d2014-10-29 17:24:25 -0700729 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700730 return resolved_method;
731 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700732 // TODO This lookup is rather slow.
733 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
734 mirror::Class* method_reference_class =
735 referring_class->GetDexCache()->GetResolvedType(method_type_idx);
736 if (method_reference_class == nullptr) {
737 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000738 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700739 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700740 // It is not an interface. If the referring class is in the class hierarchy of the
741 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
742 // resolve the method.
743 if (!method_reference_class->IsAssignableFrom(referring_class)) {
744 return nullptr;
745 }
746 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700747 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
748 // The super class does not have the method.
749 return nullptr;
750 }
751 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), sizeof(void*));
752 } else {
753 return method_reference_class->FindVirtualMethodForInterfaceSuper(
754 resolved_method, sizeof(void*));
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000755 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700756 } else {
757 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700758 return this_object->GetClass()->GetVTableEntry(
759 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700760 }
761}
762
Mathieu Chartiere401d142015-04-22 13:56:20 -0700763inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
764 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700765 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
766 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
767 if (UNLIKELY(klass == nullptr)) {
768 CHECK(self->IsExceptionPending());
769 return nullptr; // Failure - Indicate to caller to deliver exception
770 }
771 // Perform access check if necessary.
772 mirror::Class* referring_class = referrer->GetDeclaringClass();
773 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
774 ThrowIllegalAccessErrorClass(referring_class, klass);
775 return nullptr; // Failure - Indicate to caller to deliver exception
776 }
777 // If we're just implementing const-class, we shouldn't call <clinit>.
778 if (!can_run_clinit) {
779 return klass;
780 }
781 // If we are the <clinit> of this class, just return our storage.
782 //
783 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
784 // running.
785 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
786 return klass;
787 }
788 StackHandleScope<1> hs(self);
789 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700790 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700791 CHECK(self->IsExceptionPending());
792 return nullptr; // Failure - Indicate to caller to deliver exception
793 }
794 return h_class.Get();
795}
796
Mathieu Chartiere401d142015-04-22 13:56:20 -0700797inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700798 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
799 return class_linker->ResolveString(string_idx, referrer);
800}
801
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800802inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700803 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700804 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700805 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000806 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700807 self->ClearException();
808 }
809 // Decode locked object and unlock, before popping local references.
810 self->DecodeJObject(locked)->MonitorExit(self);
811 if (UNLIKELY(self->IsExceptionPending())) {
812 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
813 << saved_exception->Dump()
814 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000815 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700816 }
817 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700818 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000819 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700820 }
821}
822
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700823template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800824inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700825 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
826 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
827 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
828 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
829 if (LIKELY(f > kMinIntAsFloat)) {
830 if (LIKELY(f < kMaxIntAsFloat)) {
831 return static_cast<INT_TYPE>(f);
832 } else {
833 return kMaxInt;
834 }
835 } else {
836 return (f != f) ? 0 : kMinInt; // f != f implies NaN
837 }
838}
839
840} // namespace art
841
842#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_