blob: 7ecd59527bef49868e21d19caeb2c4613322acd9 [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
Matthew Gharrity465ecc82016-07-19 21:32:52 +000022#include "art_method-inl.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 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700489 // Resolution and access check.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700490 if (UNLIKELY(resolved_method == nullptr)) {
491 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
492 return nullptr; // Failure.
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700493 } else if (access_check) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700494 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700495 bool can_access_resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700496 referrer->GetDeclaringClass()->CheckResolvedMethodAccess<type>(methods_class,
497 resolved_method,
498 method_idx);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700499 if (UNLIKELY(!can_access_resolved_method)) {
500 DCHECK(self->IsExceptionPending()); // Throw exception and unwind.
501 return nullptr; // Failure.
502 }
Nicolas Geoffray470d54f2015-10-02 17:14:53 +0100503 // Incompatible class change should have been handled in resolve method.
504 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
505 ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
506 referrer);
507 return nullptr; // Failure.
508 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700509 }
Aart Bik2a1b7ac2016-06-29 14:54:26 -0700510 // Next, null pointer check.
511 if (UNLIKELY(*this_object == nullptr && type != kStatic)) {
512 if (UNLIKELY(resolved_method->GetDeclaringClass()->IsStringClass() &&
513 resolved_method->IsConstructor())) {
514 // Hack for String init:
515 //
516 // We assume that the input of String.<init> in verified code is always
517 // an unitialized reference. If it is a null constant, it must have been
518 // optimized out by the compiler. Do not throw NullPointerException.
519 } else {
520 // Maintain interpreter-like semantics where NullPointerException is thrown
521 // after potential NoSuchMethodError from class linker.
522 ThrowNullPointerExceptionForMethodAccess(method_idx, type);
523 return nullptr; // Failure.
524 }
525 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700526 switch (type) {
527 case kStatic:
528 case kDirect:
529 return resolved_method;
530 case kVirtual: {
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700531 mirror::Class* klass = (*this_object)->GetClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700532 uint16_t vtable_index = resolved_method->GetMethodIndex();
533 if (access_check &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700534 (!klass->HasVTable() ||
535 vtable_index >= static_cast<uint32_t>(klass->GetVTableLength()))) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700536 // Behavior to agree with that of the verifier.
537 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
538 resolved_method->GetName(), resolved_method->GetSignature());
539 return nullptr; // Failure.
540 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700541 DCHECK(klass->HasVTable()) << PrettyClass(klass);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542 return klass->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700543 }
544 case kSuper: {
Alex Light705ad492015-09-21 11:36:30 -0700545 // TODO This lookup is quite slow.
546 // NB This is actually quite tricky to do any other way. We cannot use GetDeclaringClass since
547 // that will actually not be what we want in some cases where there are miranda methods or
548 // defaults. What we actually need is a GetContainingClass that says which classes virtuals
549 // this method is coming from.
550 mirror::Class* referring_class = referrer->GetDeclaringClass();
551 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
552 mirror::Class* method_reference_class = class_linker->ResolveType(method_type_idx, referrer);
553 if (UNLIKELY(method_reference_class == nullptr)) {
554 // Bad type idx.
555 CHECK(self->IsExceptionPending());
556 return nullptr;
557 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700558 // It is not an interface. If the referring class is in the class hierarchy of the
559 // referenced class in the bytecode, we use its super class. Otherwise, we throw
560 // a NoSuchMethodError.
561 mirror::Class* super_class = nullptr;
562 if (method_reference_class->IsAssignableFrom(referring_class)) {
563 super_class = referring_class->GetSuperClass();
564 }
Alex Light705ad492015-09-21 11:36:30 -0700565 uint16_t vtable_index = resolved_method->GetMethodIndex();
566 if (access_check) {
567 // Check existence of super class.
Aart Bikf663e342016-04-04 17:28:59 -0700568 if (super_class == nullptr ||
569 !super_class->HasVTable() ||
Alex Light705ad492015-09-21 11:36:30 -0700570 vtable_index >= static_cast<uint32_t>(super_class->GetVTableLength())) {
571 // Behavior to agree with that of the verifier.
572 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
573 resolved_method->GetName(), resolved_method->GetSignature());
574 return nullptr; // Failure.
575 }
576 }
577 DCHECK(super_class != nullptr);
578 DCHECK(super_class->HasVTable());
579 return super_class->GetVTableEntry(vtable_index, class_linker->GetImagePointerSize());
580 } else {
581 // It is an interface.
582 if (access_check) {
583 if (!method_reference_class->IsAssignableFrom((*this_object)->GetClass())) {
584 ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(resolved_method,
585 method_reference_class,
586 *this_object,
587 referrer);
588 return nullptr; // Failure.
589 }
590 }
591 // TODO We can do better than this for a (compiled) fastpath.
592 ArtMethod* result = method_reference_class->FindVirtualMethodForInterfaceSuper(
593 resolved_method, class_linker->GetImagePointerSize());
594 // Throw an NSME if nullptr;
595 if (result == nullptr) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700596 ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(),
597 resolved_method->GetName(), resolved_method->GetSignature());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700598 }
Alex Light705ad492015-09-21 11:36:30 -0700599 return result;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700600 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700601 }
602 case kInterface: {
Matthew Gharrity465ecc82016-07-19 21:32:52 +0000603 uint32_t imt_index = resolved_method->GetImtIndex();
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +0000604 size_t pointer_size = class_linker->GetImagePointerSize();
605 ArtMethod* imt_method = (*this_object)->GetClass()->GetImt(pointer_size)->
606 Get(imt_index, pointer_size);
Nicolas Geoffray796d6302016-03-13 22:22:31 +0000607 if (!imt_method->IsRuntimeMethod()) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700608 if (kIsDebugBuild) {
609 mirror::Class* klass = (*this_object)->GetClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700610 ArtMethod* method = klass->FindVirtualMethodForInterface(
611 resolved_method, class_linker->GetImagePointerSize());
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700612 CHECK_EQ(imt_method, method) << PrettyMethod(resolved_method) << " / " <<
613 PrettyMethod(imt_method) << " / " << PrettyMethod(method) << " / " <<
614 PrettyClass(klass);
615 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700616 return imt_method;
617 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700618 ArtMethod* interface_method = (*this_object)->GetClass()->FindVirtualMethodForInterface(
619 resolved_method, class_linker->GetImagePointerSize());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700620 if (UNLIKELY(interface_method == nullptr)) {
621 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method,
Andreas Gampe3a357142015-08-07 17:20:11 -0700622 *this_object, referrer);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700623 return nullptr; // Failure.
624 }
625 return interface_method;
626 }
627 }
628 default:
629 LOG(FATAL) << "Unknown invoke type " << type;
630 return nullptr; // Failure.
631 }
632}
633
634// Explicit template declarations of FindMethodFromCode for all invoke types.
635#define EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, _access_check) \
Mathieu Chartier90443472015-07-16 20:32:27 -0700636 template SHARED_REQUIRES(Locks::mutator_lock_) ALWAYS_INLINE \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700637 ArtMethod* FindMethodFromCode<_type, _access_check>(uint32_t method_idx, \
638 mirror::Object** this_object, \
Andreas Gampe3a357142015-08-07 17:20:11 -0700639 ArtMethod* referrer, \
Mathieu Chartiere401d142015-04-22 13:56:20 -0700640 Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700641#define EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(_type) \
642 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, false); \
643 EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL(_type, true)
644
645EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kStatic);
646EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kDirect);
647EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kVirtual);
648EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kSuper);
649EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL(kInterface);
650
651#undef EXPLICIT_FIND_METHOD_FROM_CODE_TYPED_TEMPLATE_DECL
652#undef EXPLICIT_FIND_METHOD_FROM_CODE_TEMPLATE_DECL
653
654// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700655inline ArtField* FindFieldFast(uint32_t field_idx, ArtMethod* referrer, FindFieldType type,
656 size_t expected_size) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700657 ArtField* resolved_field =
658 referrer->GetDeclaringClass()->GetDexCache()->GetResolvedField(field_idx, sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700659 if (UNLIKELY(resolved_field == nullptr)) {
660 return nullptr;
661 }
662 // Check for incompatible class change.
663 bool is_primitive;
664 bool is_set;
665 bool is_static;
666 switch (type) {
667 case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break;
668 case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break;
669 case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break;
670 case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break;
671 case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break;
672 case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break;
673 case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break;
674 case StaticPrimitiveWrite: is_primitive = true; is_set = true; is_static = true; break;
675 default:
Ian Rogers2c4257b2014-10-24 14:20:06 -0700676 LOG(FATAL) << "UNREACHABLE";
677 UNREACHABLE();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700678 }
679 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
680 // Incompatible class change.
681 return nullptr;
682 }
683 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
684 if (is_static) {
685 // Check class is initialized else fail so that we can contend to initialize the class with
686 // other threads that may be racing to do this.
687 if (UNLIKELY(!fields_class->IsInitialized())) {
688 return nullptr;
689 }
690 }
691 mirror::Class* referring_class = referrer->GetDeclaringClass();
692 if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
Mathieu Chartiere401d142015-04-22 13:56:20 -0700693 !referring_class->CanAccessMember(fields_class, resolved_field->GetAccessFlags()) ||
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700694 (is_set && resolved_field->IsFinal() && (fields_class != referring_class)))) {
695 // Illegal access.
696 return nullptr;
697 }
698 if (UNLIKELY(resolved_field->IsPrimitiveType() != is_primitive ||
699 resolved_field->FieldSize() != expected_size)) {
700 return nullptr;
701 }
702 return resolved_field;
703}
704
705// Fast path method resolution that can't throw exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700706inline ArtMethod* FindMethodFast(uint32_t method_idx, mirror::Object* this_object,
707 ArtMethod* referrer, bool access_check, InvokeType type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700708 if (UNLIKELY(this_object == nullptr && type != kStatic)) {
709 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700710 }
Alex Light705ad492015-09-21 11:36:30 -0700711 mirror::Class* referring_class = referrer->GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700712 ArtMethod* resolved_method =
Alex Light705ad492015-09-21 11:36:30 -0700713 referring_class->GetDexCache()->GetResolvedMethod(method_idx, sizeof(void*));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700714 if (UNLIKELY(resolved_method == nullptr)) {
715 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700716 }
717 if (access_check) {
718 // Check for incompatible class change errors and access.
719 bool icce = resolved_method->CheckIncompatibleClassChange(type);
720 if (UNLIKELY(icce)) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700721 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700722 }
723 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700724 if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
725 !referring_class->CanAccessMember(methods_class,
726 resolved_method->GetAccessFlags()))) {
727 // Potential illegal access, may need to refine the method's class.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700728 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700729 }
730 }
731 if (type == kInterface) { // Most common form of slow path dispatch.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700732 return this_object->GetClass()->FindVirtualMethodForInterface(resolved_method, sizeof(void*));
Jeff Hao207a37d2014-10-29 17:24:25 -0700733 } else if (type == kStatic || type == kDirect) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700734 return resolved_method;
735 } else if (type == kSuper) {
Alex Light705ad492015-09-21 11:36:30 -0700736 // TODO This lookup is rather slow.
737 uint16_t method_type_idx = referring_class->GetDexFile().GetMethodId(method_idx).class_idx_;
738 mirror::Class* method_reference_class =
739 referring_class->GetDexCache()->GetResolvedType(method_type_idx);
740 if (method_reference_class == nullptr) {
741 // Need to do full type resolution...
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000742 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -0700743 } else if (!method_reference_class->IsInterface()) {
Aart Bikf663e342016-04-04 17:28:59 -0700744 // It is not an interface. If the referring class is in the class hierarchy of the
745 // referenced class in the bytecode, we use its super class. Otherwise, we cannot
746 // resolve the method.
747 if (!method_reference_class->IsAssignableFrom(referring_class)) {
748 return nullptr;
749 }
750 mirror::Class* super_class = referring_class->GetSuperClass();
Alex Light705ad492015-09-21 11:36:30 -0700751 if (resolved_method->GetMethodIndex() >= super_class->GetVTableLength()) {
752 // The super class does not have the method.
753 return nullptr;
754 }
755 return super_class->GetVTableEntry(resolved_method->GetMethodIndex(), sizeof(void*));
756 } else {
757 return method_reference_class->FindVirtualMethodForInterfaceSuper(
758 resolved_method, sizeof(void*));
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000759 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700760 } else {
761 DCHECK(type == kVirtual);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700762 return this_object->GetClass()->GetVTableEntry(
763 resolved_method->GetMethodIndex(), sizeof(void*));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700764 }
765}
766
Mathieu Chartiere401d142015-04-22 13:56:20 -0700767inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, ArtMethod* referrer, Thread* self,
768 bool can_run_clinit, bool verify_access) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700769 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
770 mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
771 if (UNLIKELY(klass == nullptr)) {
772 CHECK(self->IsExceptionPending());
773 return nullptr; // Failure - Indicate to caller to deliver exception
774 }
775 // Perform access check if necessary.
776 mirror::Class* referring_class = referrer->GetDeclaringClass();
777 if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
778 ThrowIllegalAccessErrorClass(referring_class, klass);
779 return nullptr; // Failure - Indicate to caller to deliver exception
780 }
781 // If we're just implementing const-class, we shouldn't call <clinit>.
782 if (!can_run_clinit) {
783 return klass;
784 }
785 // If we are the <clinit> of this class, just return our storage.
786 //
787 // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
788 // running.
789 if (klass == referring_class && referrer->IsConstructor() && referrer->IsStatic()) {
790 return klass;
791 }
792 StackHandleScope<1> hs(self);
793 Handle<mirror::Class> h_class(hs.NewHandle(klass));
Ian Rogers7b078e82014-09-10 14:44:24 -0700794 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700795 CHECK(self->IsExceptionPending());
796 return nullptr; // Failure - Indicate to caller to deliver exception
797 }
798 return h_class.Get();
799}
800
Mathieu Chartiere401d142015-04-22 13:56:20 -0700801inline mirror::String* ResolveStringFromCode(ArtMethod* referrer, uint32_t string_idx) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700802 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
803 return class_linker->ResolveString(string_idx, referrer);
804}
805
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800806inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700807 // Save any pending exception over monitor exit call.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700808 mirror::Throwable* saved_exception = nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700809 if (UNLIKELY(self->IsExceptionPending())) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000810 saved_exception = self->GetException();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700811 self->ClearException();
812 }
813 // Decode locked object and unlock, before popping local references.
814 self->DecodeJObject(locked)->MonitorExit(self);
815 if (UNLIKELY(self->IsExceptionPending())) {
816 LOG(FATAL) << "Synchronized JNI code returning with an exception:\n"
817 << saved_exception->Dump()
818 << "\nEncountered second exception during implicit MonitorExit:\n"
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000819 << self->GetException()->Dump();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700820 }
821 // Restore pending exception.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700822 if (saved_exception != nullptr) {
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000823 self->SetException(saved_exception);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700824 }
825}
826
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700827template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800828inline INT_TYPE art_float_to_integral(FLOAT_TYPE f) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700829 const INT_TYPE kMaxInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::max());
830 const INT_TYPE kMinInt = static_cast<INT_TYPE>(std::numeric_limits<INT_TYPE>::min());
831 const FLOAT_TYPE kMaxIntAsFloat = static_cast<FLOAT_TYPE>(kMaxInt);
832 const FLOAT_TYPE kMinIntAsFloat = static_cast<FLOAT_TYPE>(kMinInt);
833 if (LIKELY(f > kMinIntAsFloat)) {
834 if (LIKELY(f < kMaxIntAsFloat)) {
835 return static_cast<INT_TYPE>(f);
836 } else {
837 return kMaxInt;
838 }
839 } else {
840 return (f != f) ? 0 : kMinInt; // f != f implies NaN
841 }
842}
843
844} // namespace art
845
846#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_INL_H_