Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_REFLECTION_H_ |
| 18 | #define ART_RUNTIME_REFLECTION_H_ |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 19 | |
liulvping | fff1d8f | 2020-12-21 09:43:37 +0800 | [diff] [blame] | 20 | #include "base/enums.h" |
Andreas Gampe | 7fbc4a5 | 2018-11-28 08:26:47 -0800 | [diff] [blame] | 21 | #include "base/locks.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 22 | #include "dex/primitive.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 23 | #include "jni.h" |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 24 | #include "obj_ptr.h" |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | namespace mirror { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 28 | class Class; |
| 29 | class Object; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | } // namespace mirror |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 31 | class ArtField; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 32 | class ArtMethod; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | union JValue; |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 34 | class ScopedObjectAccessAlreadyRunnable; |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 35 | class ShadowFrame; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 36 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 37 | ObjPtr<mirror::Object> BoxPrimitive(Primitive::Type src_class, const JValue& value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 38 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 39 | |
| 40 | bool UnboxPrimitiveForField(ObjPtr<mirror::Object> o, |
| 41 | ObjPtr<mirror::Class> dst_class, |
| 42 | ArtField* f, |
Ian Rogers | 84956ff | 2014-03-26 23:52:41 -0700 | [diff] [blame] | 43 | JValue* unboxed_value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 44 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 45 | |
| 46 | bool UnboxPrimitiveForResult(ObjPtr<mirror::Object> o, |
| 47 | ObjPtr<mirror::Class> dst_class, |
| 48 | JValue* unboxed_value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 49 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 50 | |
Orion Hodson | ba28f9f | 2016-10-26 10:56:25 +0100 | [diff] [blame] | 51 | ALWAYS_INLINE bool ConvertPrimitiveValueNoThrow(Primitive::Type src_class, |
| 52 | Primitive::Type dst_class, |
| 53 | const JValue& src, |
| 54 | JValue* dst) |
| 55 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 56 | |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 57 | ALWAYS_INLINE bool ConvertPrimitiveValue(bool unbox_for_result, |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 58 | Primitive::Type src_class, |
| 59 | Primitive::Type dst_class, |
| 60 | const JValue& src, |
| 61 | JValue* dst) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 62 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 63 | |
Alex Light | 01fbfbe | 2019-06-27 10:47:04 -0700 | [diff] [blame] | 64 | // Invokes the given method (either an ArtMethod or a jmethodID) with direct/static semantics. |
| 65 | template<typename MethodType> |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 66 | JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, |
| 67 | jobject obj, |
Alex Light | 01fbfbe | 2019-06-27 10:47:04 -0700 | [diff] [blame] | 68 | MethodType mid, |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 69 | va_list args) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 70 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 71 | |
Alex Light | 01fbfbe | 2019-06-27 10:47:04 -0700 | [diff] [blame] | 72 | // Invokes the given method (either an ArtMethod or a jmethodID) with reflection semantics. |
| 73 | template<typename MethodType> |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 74 | JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, |
| 75 | jobject obj, |
Alex Light | 01fbfbe | 2019-06-27 10:47:04 -0700 | [diff] [blame] | 76 | MethodType mid, |
Elliott Hughes | 22352f3 | 2018-06-15 17:33:58 -0700 | [diff] [blame] | 77 | const jvalue* args) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 78 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 79 | |
Alex Light | 01fbfbe | 2019-06-27 10:47:04 -0700 | [diff] [blame] | 80 | // Invokes the given method (either an ArtMethod or a jmethodID) with virtual/interface semantics. |
| 81 | // Note this will perform lookup based on the 'obj' to determine which implementation of the given |
| 82 | // method should be invoked. |
| 83 | template<typename MethodType> |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 84 | JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 85 | jobject obj, |
Alex Light | 01fbfbe | 2019-06-27 10:47:04 -0700 | [diff] [blame] | 86 | MethodType mid, |
Elliott Hughes | 22352f3 | 2018-06-15 17:33:58 -0700 | [diff] [blame] | 87 | const jvalue* args) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 88 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 89 | |
Alex Light | 01fbfbe | 2019-06-27 10:47:04 -0700 | [diff] [blame] | 90 | // Invokes the given method (either an ArtMethod or a jmethodID) with virtual/interface semantics. |
| 91 | // Note this will perform lookup based on the 'obj' to determine which implementation of the given |
| 92 | // method should be invoked. |
| 93 | template<typename MethodType> |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 94 | JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 95 | jobject obj, |
Alex Light | 01fbfbe | 2019-06-27 10:47:04 -0700 | [diff] [blame] | 96 | MethodType mid, |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 97 | va_list args) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 98 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 99 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 100 | // num_frames is number of frames we look up for access check. |
liulvping | fff1d8f | 2020-12-21 09:43:37 +0800 | [diff] [blame] | 101 | template<PointerSize pointer_size> |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 102 | jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, |
| 103 | jobject method, |
| 104 | jobject receiver, |
| 105 | jobject args, |
| 106 | size_t num_frames = 1) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 107 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 108 | |
Andreas Gampe | 8ad7a3b | 2017-05-22 16:08:52 -0700 | [diff] [blame] | 109 | // Special-casing of the above. Assumes that the method is the correct constructor, the class is |
| 110 | // initialized, and that the receiver is an instance of the class. |
| 111 | void InvokeConstructor(const ScopedObjectAccessAlreadyRunnable& soa, |
| 112 | ArtMethod* constructor, |
| 113 | ObjPtr<mirror::Object> receiver, |
| 114 | jobject args) |
| 115 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 116 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 117 | ALWAYS_INLINE bool VerifyObjectIsClass(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 118 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 119 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 120 | bool VerifyAccess(Thread* self, |
| 121 | ObjPtr<mirror::Object> obj, |
| 122 | ObjPtr<mirror::Class> declaring_class, |
| 123 | uint32_t access_flags, |
| 124 | ObjPtr<mirror::Class>* calling_class, |
| 125 | size_t num_frames) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 126 | REQUIRES_SHARED(Locks::mutator_lock_); |
Jeff Hao | 11d5d8f | 2014-03-26 15:08:20 -0700 | [diff] [blame] | 127 | |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 128 | // This version takes a known calling class. |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 129 | bool VerifyAccess(ObjPtr<mirror::Object> obj, |
| 130 | ObjPtr<mirror::Class> declaring_class, |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 131 | uint32_t access_flags, |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 132 | ObjPtr<mirror::Class> calling_class) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 133 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 134 | |
| 135 | // Get the calling class by using a stack visitor, may return null for unattached native threads. |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 136 | ObjPtr<mirror::Class> GetCallingClass(Thread* self, size_t num_frames) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 137 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | f36cb5f | 2015-04-24 16:55:16 -0700 | [diff] [blame] | 138 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 139 | void InvalidReceiverError(ObjPtr<mirror::Object> o, ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 140 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 141 | |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 142 | void UpdateReference(Thread* self, jobject obj, ObjPtr<mirror::Object> result) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 143 | REQUIRES_SHARED(Locks::mutator_lock_); |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 144 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 145 | } // namespace art |
| 146 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 147 | #endif // ART_RUNTIME_REFLECTION_H_ |