Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
buzbee | 5433072 | 2011-08-23 16:46:55 -0700 | [diff] [blame] | 16 | |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_ |
| 18 | #define ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_ |
Ian Rogers | 450dcb5 | 2013-09-20 17:36:02 -0700 | [diff] [blame] | 19 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 20 | #include <jni.h> |
| 21 | #include <stdint.h> |
| 22 | |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 23 | #include "base/callee_save_type.h" |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 24 | #include "base/locks.h" |
Ian Rogers | 450dcb5 | 2013-09-20 17:36:02 -0700 | [diff] [blame] | 25 | #include "base/macros.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 26 | #include "dex/dex_file_types.h" |
| 27 | #include "dex/dex_instruction.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 28 | #include "gc/allocator_type.h" |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 29 | #include "handle.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 30 | #include "jvalue.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 31 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 32 | namespace art { |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 33 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | namespace mirror { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 35 | class Array; |
| 36 | class Class; |
Orion Hodson | dbaa5c7 | 2018-05-10 08:22:46 +0100 | [diff] [blame] | 37 | class MethodHandle; |
Orion Hodson | 18259d7 | 2018-04-12 11:18:23 +0100 | [diff] [blame] | 38 | class MethodType; |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 39 | class Object; |
| 40 | class String; |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 41 | } // namespace mirror |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 42 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 43 | class ArtField; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 44 | class ArtMethod; |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 45 | class HandleScope; |
Andreas Gampe | 04c6ab9 | 2017-06-08 21:49:14 -0700 | [diff] [blame] | 46 | enum InvokeType : uint32_t; |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 47 | class MethodReference; |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 48 | class OatQuickMethodHeader; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 49 | class ScopedObjectAccessAlreadyRunnable; |
| 50 | class Thread; |
| 51 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 52 | // Given the context of a calling Method, use its DexCache to resolve a type to a Class. If it |
| 53 | // cannot be resolved, throw an error. If it can, use it to create an instance. |
Vladimir Marko | 9b81ac3 | 2019-05-16 16:47:08 +0100 | [diff] [blame] | 54 | template <bool kInstrumented = true> |
Vladimir Marko | 4bb2af5 | 2019-03-22 11:09:19 +0000 | [diff] [blame] | 55 | ALWAYS_INLINE inline ObjPtr<mirror::Object> AllocObjectFromCode(ObjPtr<mirror::Class> klass, |
| 56 | Thread* self, |
| 57 | gc::AllocatorType allocator_type) |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 58 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 59 | REQUIRES(!Roles::uninterruptible_); |
Hiroshi Yamauchi | 3b4c189 | 2013-09-12 21:33:12 -0700 | [diff] [blame] | 60 | |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 61 | // Given the context of a calling Method and a resolved class, create an instance. |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 62 | template <bool kInstrumented> |
Vladimir Marko | 4bb2af5 | 2019-03-22 11:09:19 +0000 | [diff] [blame] | 63 | ALWAYS_INLINE |
| 64 | inline ObjPtr<mirror::Object> AllocObjectFromCodeResolved(ObjPtr<mirror::Class> klass, |
| 65 | Thread* self, |
| 66 | gc::AllocatorType allocator_type) |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 67 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 68 | REQUIRES(!Roles::uninterruptible_); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 69 | |
| 70 | // Given the context of a calling Method and an initialized class, create an instance. |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 71 | template <bool kInstrumented> |
Vladimir Marko | 4bb2af5 | 2019-03-22 11:09:19 +0000 | [diff] [blame] | 72 | ALWAYS_INLINE |
| 73 | inline ObjPtr<mirror::Object> AllocObjectFromCodeInitialized(ObjPtr<mirror::Class> klass, |
| 74 | Thread* self, |
| 75 | gc::AllocatorType allocator_type) |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 76 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 77 | REQUIRES(!Roles::uninterruptible_); |
Hiroshi Yamauchi | be1ca55 | 2014-01-15 11:46:48 -0800 | [diff] [blame] | 78 | |
| 79 | |
Mathieu Chartier | cbb2d20 | 2013-11-14 17:45:16 -0800 | [diff] [blame] | 80 | template <bool kAccessCheck> |
Vladimir Marko | 4bb2af5 | 2019-03-22 11:09:19 +0000 | [diff] [blame] | 81 | ALWAYS_INLINE inline ObjPtr<mirror::Class> CheckArrayAlloc(dex::TypeIndex type_idx, |
| 82 | int32_t component_count, |
| 83 | ArtMethod* method, |
| 84 | bool* slow_path) |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 85 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 86 | REQUIRES(!Roles::uninterruptible_); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 87 | |
| 88 | // Given the context of a calling Method, use its DexCache to resolve a type to an array Class. If |
| 89 | // it cannot be resolved, throw an error. If it can, use it to create an array. |
| 90 | // When verification/compiler hasn't been able to verify access, optionally perform an access |
| 91 | // check. |
Vladimir Marko | 9b81ac3 | 2019-05-16 16:47:08 +0100 | [diff] [blame] | 92 | template <bool kAccessCheck, bool kInstrumented = true> |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 93 | ALWAYS_INLINE inline ObjPtr<mirror::Array> AllocArrayFromCode(dex::TypeIndex type_idx, |
| 94 | int32_t component_count, |
| 95 | ArtMethod* method, |
| 96 | Thread* self, |
| 97 | gc::AllocatorType allocator_type) |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 98 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 99 | REQUIRES(!Roles::uninterruptible_); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 100 | |
Nicolas Geoffray | e761bcc | 2017-01-19 08:59:37 +0000 | [diff] [blame] | 101 | template <bool kInstrumented> |
Vladimir Marko | 4bb2af5 | 2019-03-22 11:09:19 +0000 | [diff] [blame] | 102 | ALWAYS_INLINE |
| 103 | inline ObjPtr<mirror::Array> AllocArrayFromCodeResolved(ObjPtr<mirror::Class> klass, |
| 104 | int32_t component_count, |
| 105 | Thread* self, |
| 106 | gc::AllocatorType allocator_type) |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 107 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 108 | REQUIRES(!Roles::uninterruptible_); |
Hiroshi Yamauchi | bb8f0ab | 2014-01-27 16:50:29 -0800 | [diff] [blame] | 109 | |
David Srbecky | ce32c10 | 2018-08-31 07:21:07 +0100 | [diff] [blame] | 110 | enum FindFieldFlags { |
| 111 | InstanceBit = 1 << 0, |
| 112 | StaticBit = 1 << 1, |
| 113 | ObjectBit = 1 << 2, |
| 114 | PrimitiveBit = 1 << 3, |
| 115 | ReadBit = 1 << 4, |
| 116 | WriteBit = 1 << 5, |
| 117 | }; |
| 118 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 119 | // Type of find field operation for fast and slow case. |
| 120 | enum FindFieldType { |
David Srbecky | ce32c10 | 2018-08-31 07:21:07 +0100 | [diff] [blame] | 121 | InstanceObjectRead = InstanceBit | ObjectBit | ReadBit, |
| 122 | InstanceObjectWrite = InstanceBit | ObjectBit | WriteBit, |
| 123 | InstancePrimitiveRead = InstanceBit | PrimitiveBit | ReadBit, |
| 124 | InstancePrimitiveWrite = InstanceBit | PrimitiveBit | WriteBit, |
| 125 | StaticObjectRead = StaticBit | ObjectBit | ReadBit, |
| 126 | StaticObjectWrite = StaticBit | ObjectBit | WriteBit, |
| 127 | StaticPrimitiveRead = StaticBit | PrimitiveBit | ReadBit, |
| 128 | StaticPrimitiveWrite = StaticBit | PrimitiveBit | WriteBit, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 131 | template<FindFieldType type, bool access_check> |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 132 | inline ArtField* FindFieldFromCode(uint32_t field_idx, |
| 133 | ArtMethod* referrer, |
| 134 | Thread* self, |
| 135 | size_t expected_size) |
| 136 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 137 | REQUIRES(!Roles::uninterruptible_); |
Sebastien Hertz | d4beb6b | 2013-10-02 17:07:20 +0200 | [diff] [blame] | 138 | |
| 139 | template<InvokeType type, bool access_check> |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 140 | inline ArtMethod* FindMethodFromCode(uint32_t method_idx, |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 141 | ObjPtr<mirror::Object>* this_object, |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 142 | ArtMethod* referrer, |
| 143 | Thread* self) |
| 144 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 145 | REQUIRES(!Roles::uninterruptible_); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 146 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 147 | // Fast path field resolution that can't initialize classes or throw exceptions. |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 148 | inline ArtField* FindFieldFast(uint32_t field_idx, |
| 149 | ArtMethod* referrer, |
| 150 | FindFieldType type, |
| 151 | size_t expected_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 152 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 153 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 154 | // Fast path method resolution that can't throw exceptions. |
Vladimir Marko | f79aa7f | 2017-07-04 16:58:55 +0100 | [diff] [blame] | 155 | template <InvokeType type, bool access_check> |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 156 | inline ArtMethod* FindMethodFast(uint32_t method_idx, |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 157 | ObjPtr<mirror::Object> this_object, |
Vladimir Marko | f79aa7f | 2017-07-04 16:58:55 +0100 | [diff] [blame] | 158 | ArtMethod* referrer) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 159 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 160 | |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 161 | inline ObjPtr<mirror::Class> ResolveVerifyAndClinit(dex::TypeIndex type_idx, |
| 162 | ArtMethod* referrer, |
| 163 | Thread* self, |
| 164 | bool can_run_clinit, |
| 165 | bool verify_access) |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 166 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 167 | REQUIRES(!Roles::uninterruptible_); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 168 | |
Orion Hodson | dbaa5c7 | 2018-05-10 08:22:46 +0100 | [diff] [blame] | 169 | ObjPtr<mirror::MethodHandle> ResolveMethodHandleFromCode(ArtMethod* referrer, |
| 170 | uint32_t method_handle_idx) |
| 171 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 172 | REQUIRES(!Roles::uninterruptible_); |
| 173 | |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 174 | ObjPtr<mirror::MethodType> ResolveMethodTypeFromCode(ArtMethod* referrer, dex::ProtoIndex proto_idx) |
Orion Hodson | 18259d7 | 2018-04-12 11:18:23 +0100 | [diff] [blame] | 175 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 176 | REQUIRES(!Roles::uninterruptible_); |
| 177 | |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 178 | void CheckReferenceResult(Handle<mirror::Object> o, Thread* self) |
| 179 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 180 | REQUIRES(!Roles::uninterruptible_); |
TDYa127 | 3d71d80 | 2012-08-15 03:47:03 -0700 | [diff] [blame] | 181 | |
Vladimir Marko | 4bb2af5 | 2019-03-22 11:09:19 +0000 | [diff] [blame] | 182 | JValue InvokeProxyInvocationHandler(ScopedObjectAccessAlreadyRunnable& soa, |
| 183 | const char* shorty, |
| 184 | jobject rcvr_jobj, |
| 185 | jobject interface_art_method_jobj, |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 186 | std::vector<jvalue>& args) |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 187 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 188 | REQUIRES(!Roles::uninterruptible_); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 189 | |
Mathieu Chartier | ef41db7 | 2016-10-25 15:08:01 -0700 | [diff] [blame] | 190 | bool FillArrayData(ObjPtr<mirror::Object> obj, const Instruction::ArrayDataPayload* payload) |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 191 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 192 | REQUIRES(!Roles::uninterruptible_); |
Ian Rogers | 832336b | 2014-10-08 15:35:22 -0700 | [diff] [blame] | 193 | |
Ian Rogers | 450dcb5 | 2013-09-20 17:36:02 -0700 | [diff] [blame] | 194 | template <typename INT_TYPE, typename FLOAT_TYPE> |
Andreas Gampe | 9f612ff | 2014-11-24 13:42:22 -0800 | [diff] [blame] | 195 | inline INT_TYPE art_float_to_integral(FLOAT_TYPE f); |
Ian Rogers | 450dcb5 | 2013-09-20 17:36:02 -0700 | [diff] [blame] | 196 | |
Nicolas Geoffray | 1920c10 | 2015-09-29 18:00:03 +0000 | [diff] [blame] | 197 | ArtMethod* GetCalleeSaveMethodCaller(ArtMethod** sp, |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 198 | CalleeSaveType type, |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 199 | bool do_caller_check = false) |
| 200 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 201 | |
Vladimir Marko | 6bec91c | 2017-01-09 15:03:12 +0000 | [diff] [blame] | 202 | struct CallerAndOuterMethod { |
| 203 | ArtMethod* caller; |
| 204 | ArtMethod* outer_method; |
| 205 | }; |
| 206 | |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 207 | CallerAndOuterMethod GetCalleeSaveMethodCallerAndOuterMethod(Thread* self, CalleeSaveType type) |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 208 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | 1920c10 | 2015-09-29 18:00:03 +0000 | [diff] [blame] | 209 | |
Andreas Gampe | 8228cdf | 2017-05-30 15:03:54 -0700 | [diff] [blame] | 210 | ArtMethod* GetCalleeSaveOuterMethod(Thread* self, CalleeSaveType type) |
Nicolas Geoffray | 5b3c6c0 | 2017-01-19 14:22:26 +0000 | [diff] [blame] | 211 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 212 | |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 213 | // Returns whether we need to do class initialization check before invoking the method. |
| 214 | // The caller is responsible for performing that check. |
| 215 | bool NeedsClinitCheckBeforeCall(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); |
| 216 | |
Vladimir Marko | cedec9d | 2021-02-08 16:16:13 +0000 | [diff] [blame] | 217 | // Returns the synchronization object for a native method for a GenericJni frame |
| 218 | // we have just created or are about to exit. The synchronization object is |
| 219 | // the class object for static methods and the `this` object otherwise. |
| 220 | jobject GetGenericJniSynchronizationObject(Thread* self, ArtMethod* called) |
| 221 | REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | 6e043bb | 2020-02-10 16:56:54 +0000 | [diff] [blame] | 222 | |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 223 | // Update .bss method entrypoint if the `callee_reference` has an associated oat file |
| 224 | // and that oat file has a .bss entry for the `callee_reference`. |
| 225 | void MaybeUpdateBssMethodEntry(ArtMethod* callee, MethodReference callee_reference); |
| 226 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 227 | } // namespace art |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 228 | |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 229 | #endif // ART_RUNTIME_ENTRYPOINTS_ENTRYPOINT_UTILS_H_ |