blob: c8bf6d08ab0d12cc136a053ac94d95a0b0a6840c [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
Andreas Gampe8228cdf2017-05-30 15:03:54 -070023#include "base/callee_save_type.h"
Ian Rogers450dcb52013-09-20 17:36:02 -070024#include "base/macros.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "base/mutex.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/dex_file_types.h"
27#include "dex/dex_instruction.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070028#include "gc/allocator_type.h"
Mathieu Chartierbe08cf52016-09-13 13:41:24 -070029#include "handle.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070030#include "jvalue.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070031
Shih-wei Liao2d831012011-09-28 22:06:53 -070032namespace art {
Ian Rogers848871b2013-08-05 10:56:33 -070033
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080035class Array;
36class Class;
Orion Hodsondbaa5c72018-05-10 08:22:46 +010037class MethodHandle;
Orion Hodson18259d72018-04-12 11:18:23 +010038class MethodType;
Igor Murashkin2ffb7032017-11-08 13:35:21 -080039class Object;
40class String;
Ian Rogers848871b2013-08-05 10:56:33 -070041} // namespace mirror
Ian Rogers57b86d42012-03-27 16:05:41 -070042
Mathieu Chartierc7853442015-03-27 14:35:38 -070043class ArtField;
Mathieu Chartiere401d142015-04-22 13:56:20 -070044class ArtMethod;
Andreas Gampe04c6ab92017-06-08 21:49:14 -070045enum InvokeType : uint32_t;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010046class OatQuickMethodHeader;
Mingyao Yang98d1cc82014-05-15 17:02:16 -070047class ScopedObjectAccessAlreadyRunnable;
48class Thread;
49
Ian Rogers57b86d42012-03-27 16:05:41 -070050// Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it
51// cannot be resolved, throw an error. If it can, use it to create an instance.
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +000052template <bool kInstrumented>
53ALWAYS_INLINE inline mirror::Object* AllocObjectFromCode(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080054 Thread* self,
55 gc::AllocatorType allocator_type)
Mathieu Chartierbe08cf52016-09-13 13:41:24 -070056 REQUIRES_SHARED(Locks::mutator_lock_)
57 REQUIRES(!Roles::uninterruptible_);
Hiroshi Yamauchi3b4c1892013-09-12 21:33:12 -070058
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080059// Given the context of a calling Method and a resolved class, create an instance.
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080060template <bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080061ALWAYS_INLINE inline mirror::Object* AllocObjectFromCodeResolved(mirror::Class* klass,
62 Thread* self,
63 gc::AllocatorType allocator_type)
Mathieu Chartierbe08cf52016-09-13 13:41:24 -070064 REQUIRES_SHARED(Locks::mutator_lock_)
65 REQUIRES(!Roles::uninterruptible_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080066
67// Given the context of a calling Method and an initialized class, create an instance.
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080068template <bool kInstrumented>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -070069ALWAYS_INLINE inline mirror::Object* AllocObjectFromCodeInitialized(
70 mirror::Class* klass,
71 Thread* self,
72 gc::AllocatorType allocator_type)
73 REQUIRES_SHARED(Locks::mutator_lock_)
74 REQUIRES(!Roles::uninterruptible_);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -080075
76
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080077template <bool kAccessCheck>
Andreas Gampea5b09a62016-11-17 15:21:22 -080078ALWAYS_INLINE inline mirror::Class* CheckArrayAlloc(dex::TypeIndex type_idx,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080079 int32_t component_count,
Mathieu Chartiere401d142015-04-22 13:56:20 -070080 ArtMethod* method,
Andreas Gampe9f612ff2014-11-24 13:42:22 -080081 bool* slow_path)
Mathieu Chartierbe08cf52016-09-13 13:41:24 -070082 REQUIRES_SHARED(Locks::mutator_lock_)
83 REQUIRES(!Roles::uninterruptible_);
Ian Rogers57b86d42012-03-27 16:05:41 -070084
85// Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If
86// it cannot be resolved, throw an error. If it can, use it to create an array.
87// When verification/compiler hasn't been able to verify access, optionally perform an access
88// check.
Mathieu Chartiercbb2d202013-11-14 17:45:16 -080089template <bool kAccessCheck, bool kInstrumented>
Vladimir Markobcf17522018-06-01 13:14:32 +010090ALWAYS_INLINE inline ObjPtr<mirror::Array> AllocArrayFromCode(dex::TypeIndex type_idx,
91 int32_t component_count,
92 ArtMethod* method,
93 Thread* self,
94 gc::AllocatorType allocator_type)
Mathieu Chartierbe08cf52016-09-13 13:41:24 -070095 REQUIRES_SHARED(Locks::mutator_lock_)
96 REQUIRES(!Roles::uninterruptible_);
Ian Rogers57b86d42012-03-27 16:05:41 -070097
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +000098template <bool kInstrumented>
Andreas Gampe9f612ff2014-11-24 13:42:22 -080099ALWAYS_INLINE inline mirror::Array* AllocArrayFromCodeResolved(mirror::Class* klass,
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800100 int32_t component_count,
101 Thread* self,
102 gc::AllocatorType allocator_type)
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700103 REQUIRES_SHARED(Locks::mutator_lock_)
104 REQUIRES(!Roles::uninterruptible_);
Hiroshi Yamauchibb8f0ab2014-01-27 16:50:29 -0800105
David Srbeckyce32c102018-08-31 07:21:07 +0100106enum FindFieldFlags {
107 InstanceBit = 1 << 0,
108 StaticBit = 1 << 1,
109 ObjectBit = 1 << 2,
110 PrimitiveBit = 1 << 3,
111 ReadBit = 1 << 4,
112 WriteBit = 1 << 5,
113};
114
Ian Rogers08f753d2012-08-24 14:35:25 -0700115// Type of find field operation for fast and slow case.
116enum FindFieldType {
David Srbeckyce32c102018-08-31 07:21:07 +0100117 InstanceObjectRead = InstanceBit | ObjectBit | ReadBit,
118 InstanceObjectWrite = InstanceBit | ObjectBit | WriteBit,
119 InstancePrimitiveRead = InstanceBit | PrimitiveBit | ReadBit,
120 InstancePrimitiveWrite = InstanceBit | PrimitiveBit | WriteBit,
121 StaticObjectRead = StaticBit | ObjectBit | ReadBit,
122 StaticObjectWrite = StaticBit | ObjectBit | WriteBit,
123 StaticPrimitiveRead = StaticBit | PrimitiveBit | ReadBit,
124 StaticPrimitiveWrite = StaticBit | PrimitiveBit | WriteBit,
Ian Rogers08f753d2012-08-24 14:35:25 -0700125};
126
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200127template<FindFieldType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700128inline ArtField* FindFieldFromCode(uint32_t field_idx,
129 ArtMethod* referrer,
130 Thread* self,
131 size_t expected_size)
132 REQUIRES_SHARED(Locks::mutator_lock_)
133 REQUIRES(!Roles::uninterruptible_);
Sebastien Hertzd4beb6b2013-10-02 17:07:20 +0200134
135template<InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700136inline ArtMethod* FindMethodFromCode(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700137 ObjPtr<mirror::Object>* this_object,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700138 ArtMethod* referrer,
139 Thread* self)
140 REQUIRES_SHARED(Locks::mutator_lock_)
141 REQUIRES(!Roles::uninterruptible_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700142
Ian Rogers08f753d2012-08-24 14:35:25 -0700143// Fast path field resolution that can't initialize classes or throw exceptions.
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700144inline ArtField* FindFieldFast(uint32_t field_idx,
145 ArtMethod* referrer,
146 FindFieldType type,
147 size_t expected_size)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700148 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700149
Ian Rogers08f753d2012-08-24 14:35:25 -0700150// Fast path method resolution that can't throw exceptions.
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100151template <InvokeType type, bool access_check>
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700152inline ArtMethod* FindMethodFast(uint32_t method_idx,
Mathieu Chartieref41db72016-10-25 15:08:01 -0700153 ObjPtr<mirror::Object> this_object,
Vladimir Markof79aa7f2017-07-04 16:58:55 +0100154 ArtMethod* referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700155 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700156
Vladimir Marko28e012a2017-12-07 11:22:59 +0000157inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx,
158 ArtMethod* referrer,
159 Thread* self,
160 bool can_run_clinit,
161 bool verify_access)
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700162 REQUIRES_SHARED(Locks::mutator_lock_)
163 REQUIRES(!Roles::uninterruptible_);
Ian Rogers57b86d42012-03-27 16:05:41 -0700164
Orion Hodsondbaa5c72018-05-10 08:22:46 +0100165ObjPtr<mirror::MethodHandle> ResolveMethodHandleFromCode(ArtMethod* referrer,
166 uint32_t method_handle_idx)
167 REQUIRES_SHARED(Locks::mutator_lock_)
168 REQUIRES(!Roles::uninterruptible_);
169
Orion Hodson06d10a72018-05-14 08:53:38 +0100170ObjPtr<mirror::MethodType> ResolveMethodTypeFromCode(ArtMethod* referrer, dex::ProtoIndex proto_idx)
Orion Hodson18259d72018-04-12 11:18:23 +0100171 REQUIRES_SHARED(Locks::mutator_lock_)
172 REQUIRES(!Roles::uninterruptible_);
173
Ian Rogerse5877a12014-07-16 12:06:35 -0700174// TODO: annotalysis disabled as monitor semantics are maintained in Java code.
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800175inline void UnlockJniSynchronizedMethod(jobject locked, Thread* self)
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700176 NO_THREAD_SAFETY_ANALYSIS REQUIRES(!Roles::uninterruptible_);
TDYa1273d71d802012-08-15 03:47:03 -0700177
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700178void CheckReferenceResult(Handle<mirror::Object> o, Thread* self)
179 REQUIRES_SHARED(Locks::mutator_lock_)
180 REQUIRES(!Roles::uninterruptible_);
TDYa1273d71d802012-08-15 03:47:03 -0700181
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700182JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, const char* shorty,
Brian Carlstromea46f952013-07-30 01:26:50 -0700183 jobject rcvr_jobj, jobject interface_art_method_jobj,
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800184 std::vector<jvalue>& args)
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700185 REQUIRES_SHARED(Locks::mutator_lock_)
186 REQUIRES(!Roles::uninterruptible_);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800187
Mathieu Chartieref41db72016-10-25 15:08:01 -0700188bool FillArrayData(ObjPtr<mirror::Object> obj, const Instruction::ArrayDataPayload* payload)
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700189 REQUIRES_SHARED(Locks::mutator_lock_)
190 REQUIRES(!Roles::uninterruptible_);
Ian Rogers832336b2014-10-08 15:35:22 -0700191
Ian Rogers450dcb52013-09-20 17:36:02 -0700192template <typename INT_TYPE, typename FLOAT_TYPE>
Andreas Gampe9f612ff2014-11-24 13:42:22 -0800193inline INT_TYPE art_float_to_integral(FLOAT_TYPE f);
Ian Rogers450dcb52013-09-20 17:36:02 -0700194
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000195ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp,
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700196 CalleeSaveType type,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700197 bool do_caller_check = false)
198 REQUIRES_SHARED(Locks::mutator_lock_);
199
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000200struct CallerAndOuterMethod {
201 ArtMethod* caller;
202 ArtMethod* outer_method;
203};
204
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700205CallerAndOuterMethod GetCalleeSaveMethodCallerAndOuterMethod(Thread* self, CalleeSaveType type)
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700206 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray1920c102015-09-29 18:00:03 +0000207
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700208ArtMethod* GetCalleeSaveOuterMethod(Thread* self, CalleeSaveType type)
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000209 REQUIRES_SHARED(Locks::mutator_lock_);
210
Shih-wei Liao2d831012011-09-28 22:06:53 -0700211} // namespace art
Ian Rogersad42e132011-09-17 20:23:33 -0700212
Ian Rogers7655f292013-07-29 11:07:13 -0700213#endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_