blob: 44c89adada58c3499c4bdf9def6d3cd2845ccfd0 [file] [log] [blame]
Elliott Hughes0f3c5532012-03-30 14:51:51 -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 */
buzbee54330722011-08-23 16:46:55 -070016
Ian Rogers7655f292013-07-29 11:07:13 -070017#ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_
18#define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_
Ian Rogers450dcb52013-09-20 17:36:02 -070019
Mingyao Yang98d1cc82014-05-15 17:02:16 -070020#include <jni.h>
21#include <stdint.h>
22
Ian Rogers450dcb52013-09-20 17:36:02 -070023#include "base/macros.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070024#include "base/mutex.h"
25#include "gc/allocator_type.h"
Elliott Hughes0f3c5532012-03-30 14:51:51 -070026#include "invoke_type.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070027#include "jvalue.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070028
Shih-wei Liao2d831012011-09-28 22:06:53 -070029namespace art {
Ian Rogers848871b2013-08-05 10:56:33 -070030
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031namespace mirror {
Ian Rogers848871b2013-08-05 10:56:33 -070032 class Class;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033 class Array;
Brian Carlstromea46f952013-07-30 01:26:50 -070034 class ArtField;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070035 class ArtMethod;
Ian Rogers848871b2013-08-05 10:56:33 -070036 class Object;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070037 class String;
Ian Rogers848871b2013-08-05 10:56:33 -070038} // namespace mirror
Ian Rogers57b86d42012-03-27 16:05:41 -070039
Mingyao Yang98d1cc82014-05-15 17:02:16 -070040class ScopedObjectAccessAlreadyRunnable;
41class Thread;
42
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080043template <const bool kAccessCheck>
44ALWAYS_INLINE static inline mirror::Class* CheckObjectAlloc(uint32_t type_idx,
45 mirror::ArtMethod* method,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080046 Thread* self, bool* slow_path)
Ian Rogerse5877a12014-07-16 12:06:35 -070047 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070048
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080049// TODO: Fix no thread safety analysis when annotalysis is smarter.
50ALWAYS_INLINE static inline mirror::Class* CheckClassInitializedForObjectAlloc(mirror::Class* klass,
51 Thread* self, bool* slow_path)
Ian Rogerse5877a12014-07-16 12:06:35 -070052 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080053
Ian Rogers57b86d42012-03-27 16:05:41 -070054// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
55// cannot be resolved, throw an error. If it can, use it to create an instance.
56// When verification/compiler hasn't been able to verify access, optionally perform an access
57// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080058template <bool kAccessCheck, bool kInstrumented>
59ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCode(uint32_t type_idx,
60 mirror::ArtMethod* method,
61 Thread* self,
Ian Rogerse5877a12014-07-16 12:06:35 -070062 gc::AllocatorType allocator_type)
63 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070064
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080065// Given the context of a calling Method and a resolved class, create an instance.
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080066template <bool kInstrumented>
67ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
68 mirror::ArtMethod* method,
69 Thread* self,
70 gc::AllocatorType allocator_type)
Ian Rogerse5877a12014-07-16 12:06:35 -070071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080072
73// Given the context of a calling Method and an initialized class, create an instance.
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080074template <bool kInstrumented>
75ALWAYS_INLINE static inline mirror::Object* AllocObjectFromCodeInitialized(mirror::Class* klass,
76 mirror::ArtMethod* method,
77 Thread* self,
Ian Rogerse5877a12014-07-16 12:06:35 -070078 gc::AllocatorType allocator_type)
79 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080080
81
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080082template <bool kAccessCheck>
83ALWAYS_INLINE static inline mirror::Class* CheckArrayAlloc(uint32_t type_idx,
84 mirror::ArtMethod* method,
Mathieu Chartiere6da9af2013-12-16 11:54:42 -080085 int32_t component_count,
86 bool* slow_path)
Ian Rogerse5877a12014-07-16 12:06:35 -070087 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -070088
89// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
90// it cannot be resolved, throw an error. If it can, use it to create an array.
91// When verification/compiler hasn't been able to verify access, optionally perform an access
92// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080093template <bool kAccessCheck, bool kInstrumented>
94ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCode(uint32_t type_idx,
95 mirror::ArtMethod* method,
96 int32_t component_count,
97 Thread* self,
98 gc::AllocatorType allocator_type)
Ian Rogerse5877a12014-07-16 12:06:35 -070099 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700100
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800101template <bool kAccessCheck, bool kInstrumented>
102ALWAYS_INLINE static inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
103 mirror::ArtMethod* method,
104 int32_t component_count,
105 Thread* self,
106 gc::AllocatorType allocator_type)
Ian Rogerse5877a12014-07-16 12:06:35 -0700107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800108
Brian Carlstromea46f952013-07-30 01:26:50 -0700109extern mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::ArtMethod* method,
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800110 int32_t component_count, Thread* self,
111 bool access_check,
112 gc::AllocatorType allocator_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700114
Mathieu Chartiercbb2d202013-11-14 17:45:16 -0800115extern mirror::Array* CheckAndAllocArrayFromCodeInstrumented(uint32_t type_idx,
116 mirror::ArtMethod* method,
117 int32_t component_count, Thread* self,
118 bool access_check,
119 gc::AllocatorType allocator_type)
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -0700120 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121
Ian Rogers08f753d2012-08-24 14:35:25 -0700122// Type of find field operation for fast and slow case.
123enum FindFieldType {
124 InstanceObjectRead,
125 InstanceObjectWrite,
126 InstancePrimitiveRead,
127 InstancePrimitiveWrite,
128 StaticObjectRead,
129 StaticObjectWrite,
130 StaticPrimitiveRead,
131 StaticPrimitiveWrite,
132};
133
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200134template<FindFieldType type, bool access_check>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135static inline mirror::ArtField* FindFieldFromCode(uint32_t field_idx, mirror::ArtMethod* referrer,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700136 Thread* self, size_t expected_size)
137 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200138
139template<InvokeType type, bool access_check>
Mathieu Chartierd565caf2014-02-16 15:59:00 -0800140static inline mirror::ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700141 mirror::Object** this_object,
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700142 mirror::ArtMethod** referrer, Thread* self)
143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700144
Ian Rogers08f753d2012-08-24 14:35:25 -0700145// Fast path field resolution that can't initialize classes or throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700146static inline mirror::ArtField* FindFieldFast(uint32_t field_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800147 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700148 FindFieldType type, size_t expected_size)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700150
Ian Rogers08f753d2012-08-24 14:35:25 -0700151// Fast path method resolution that can't throw exceptions.
Brian Carlstromea46f952013-07-30 01:26:50 -0700152static inline mirror::ArtMethod* FindMethodFast(uint32_t method_idx,
153 mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800154 mirror::ArtMethod* referrer,
Brian Carlstromea46f952013-07-30 01:26:50 -0700155 bool access_check, InvokeType type)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700157
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700158static inline mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159 mirror::ArtMethod* referrer,
Ian Rogersfa46d3e2013-05-15 00:16:04 -0700160 Thread* self, bool can_run_clinit,
161 bool verify_access)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700162 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700163
jeffhaod7521322012-11-21 15:38:24 -0800164extern void ThrowStackOverflowError(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
165
Ian Rogersef7d42f2014-01-06 12:55:46 -0800166static inline mirror::String* ResolveStringFromCode(mirror::ArtMethod* referrer,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 uint32_t string_idx)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700168 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Shih-wei Liao2d831012011-09-28 22:06:53 -0700169
Ian Rogerse5877a12014-07-16 12:06:35 -0700170// TODO: annotalysis disabled as monitor semantics are maintained in Java code.
TDYa1273d71d802012-08-15 03:47:03 -0700171static inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Ian Rogerse5877a12014-07-16 12:06:35 -0700172 NO_THREAD_SAFETY_ANALYSIS;
TDYa1273d71d802012-08-15 03:47:03 -0700173
Ian Rogerse5877a12014-07-16 12:06:35 -0700174void CheckReferenceResult(mirror::Object* o, Thread* self)
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
TDYa1273d71d802012-08-15 03:47:03 -0700176
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700177static inline void CheckSuspend(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
jeffhao373c52f2012-11-20 16:11:52 -0800178
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700179JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700180 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800181 std::vector<jvalue>& args)
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700182 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800183
Jeff Hao58df3272013-04-22 15:28:53 -0700184// Entry point for deoptimization.
Ian Rogers848871b2013-08-05 10:56:33 -0700185extern "C" void art_quick_deoptimize();
186static inline uintptr_t GetQuickDeoptimizationEntryPoint() {
Jeff Hao58df3272013-04-22 15:28:53 -0700187 return reinterpret_cast<uintptr_t>(art_quick_deoptimize);
188}
189
190// Return address of instrumentation stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700191extern "C" void art_quick_instrumentation_entry(void*);
192static inline void* GetQuickInstrumentationEntryPoint() {
193 return reinterpret_cast<void*>(art_quick_instrumentation_entry);
Jeff Hao58df3272013-04-22 15:28:53 -0700194}
195
196// The return_pc of instrumentation exit stub.
Ian Rogers848871b2013-08-05 10:56:33 -0700197extern "C" void art_quick_instrumentation_exit();
198static inline uintptr_t GetQuickInstrumentationExitPc() {
199 return reinterpret_cast<uintptr_t>(art_quick_instrumentation_exit);
200}
201
Brian Carlstromea46f952013-07-30 01:26:50 -0700202extern "C" void art_portable_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700203static inline const void* GetPortableToInterpreterBridge() {
204 return reinterpret_cast<void*>(art_portable_to_interpreter_bridge);
205}
206
Ian Rogersef7d42f2014-01-06 12:55:46 -0800207static inline const void* GetPortableToQuickBridge() {
208 // TODO: portable to quick bridge. Bug: 8196384
209 return GetPortableToInterpreterBridge();
210}
211
Brian Carlstromea46f952013-07-30 01:26:50 -0700212extern "C" void art_quick_to_interpreter_bridge(mirror::ArtMethod*);
Ian Rogers848871b2013-08-05 10:56:33 -0700213static inline const void* GetQuickToInterpreterBridge() {
214 return reinterpret_cast<void*>(art_quick_to_interpreter_bridge);
Jeff Hao58df3272013-04-22 15:28:53 -0700215}
216
Ian Rogersef7d42f2014-01-06 12:55:46 -0800217static inline const void* GetQuickToPortableBridge() {
218 // TODO: quick to portable bridge. Bug: 8196384
Ian Rogers848871b2013-08-05 10:56:33 -0700219 return GetQuickToInterpreterBridge();
Jeff Hao58df3272013-04-22 15:28:53 -0700220}
221
Ian Rogers848871b2013-08-05 10:56:33 -0700222extern "C" void art_portable_proxy_invoke_handler();
223static inline const void* GetPortableProxyInvokeHandler() {
224 return reinterpret_cast<void*>(art_portable_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700225}
226
Ian Rogers848871b2013-08-05 10:56:33 -0700227extern "C" void art_quick_proxy_invoke_handler();
228static inline const void* GetQuickProxyInvokeHandler() {
229 return reinterpret_cast<void*>(art_quick_proxy_invoke_handler);
Jeff Hao79fe5392013-04-24 18:41:58 -0700230}
231
Ian Rogers848871b2013-08-05 10:56:33 -0700232extern "C" void* art_jni_dlsym_lookup_stub(JNIEnv*, jobject);
Jeff Hao79fe5392013-04-24 18:41:58 -0700233static inline void* GetJniDlsymLookupStub() {
234 return reinterpret_cast<void*>(art_jni_dlsym_lookup_stub);
235}
Jeff Hao58df3272013-04-22 15:28:53 -0700236
Ian Rogers450dcb52013-09-20 17:36:02 -0700237template <typename INT_TYPE, typename FLOAT_TYPE>
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700238static inline INT_TYPE art_float_to_integral(FLOAT_TYPE f);
Ian Rogers450dcb52013-09-20 17:36:02 -0700239
Shih-wei Liao2d831012011-09-28 22:06:53 -0700240} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700241
Ian Rogers7655f292013-07-29 11:07:13 -0700242#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_